mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-11-21 16:01:32 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d834c9e93 | ||
|
|
8d282dcdf9 | ||
|
|
38446e8a0c | ||
|
|
1173f6263d | ||
|
|
f4bed2077f | ||
|
|
4a8b839780 | ||
|
|
f02bc80f15 | ||
|
|
32a95a9702 | ||
|
|
cc80401d5e | ||
|
|
8cc6b50d85 | ||
|
|
83521e82f5 | ||
|
|
ba8745a714 | ||
|
|
84bcd0e1c0 | ||
|
|
7459c16a3a | ||
|
|
3b4fb390c7 | ||
|
|
bb440a3cbb | ||
|
|
89b6d2256f |
Generated
+6
-6
@@ -3006,9 +3006,9 @@ checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
||||
|
||||
[[package]]
|
||||
name = "openssl"
|
||||
version = "0.10.80"
|
||||
version = "0.10.82"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a45fa2aa886c42762255da344f0a0d313e254066c46aad76f300c3d3da62d967"
|
||||
checksum = "4de8111e94a56adca7ebfe6c365b6d42f99deb69b74b6becacd4204e2f193aa2"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"cfg-if",
|
||||
@@ -3052,9 +3052,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "openssl-sys"
|
||||
version = "0.9.116"
|
||||
version = "0.9.118"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f28a22dc7140cda5f096e5e7724a6962ca81a7f8bfd2979f9b18c11af56318c4"
|
||||
checksum = "19c413d6ec9ebaf5e1c829d355cb3e9f783778d700f135be0921d52fce3c60ae"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"libc",
|
||||
@@ -4797,7 +4797,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "tokio-tungstenite"
|
||||
version = "0.28.0"
|
||||
source = "git+https://github.com/openai-oss-forks/tokio-tungstenite?rev=0e5b2d73aa18dd9f0a50ee9ff199d5aef7594186#0e5b2d73aa18dd9f0a50ee9ff199d5aef7594186"
|
||||
source = "git+https://github.com/openai-oss-forks/tokio-tungstenite?rev=7447c427513cfe685f236e1ac0513c4ea3dd9c0b#7447c427513cfe685f236e1ac0513c4ea3dd9c0b"
|
||||
dependencies = [
|
||||
"futures-util",
|
||||
"log",
|
||||
@@ -5006,7 +5006,7 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
|
||||
[[package]]
|
||||
name = "tungstenite"
|
||||
version = "0.27.0"
|
||||
source = "git+https://github.com/openai-oss-forks/tungstenite-rs?rev=4fffad30fe373adbdcffab9545e9e9bf4f2fc19f#4fffad30fe373adbdcffab9545e9e9bf4f2fc19f"
|
||||
source = "git+https://github.com/openai-oss-forks/tungstenite-rs?rev=bc589d684755a8c02da0fc7e58d0861cbfc306f1#bc589d684755a8c02da0fc7e58d0861cbfc306f1"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"data-encoding",
|
||||
|
||||
@@ -158,7 +158,7 @@ impl OtherRequestOutcome {
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
|
||||
pub struct UsageFilter {
|
||||
pub client_api_key_name: Option<String>,
|
||||
pub provider_account_name: Option<String>,
|
||||
pub provider_account_search: Option<String>,
|
||||
pub client_api_key_ref: Option<String>,
|
||||
pub request_id: Option<String>,
|
||||
pub provider_account_ref: Option<String>,
|
||||
|
||||
@@ -32,7 +32,7 @@ impl DashboardQuery {
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct UsageQuery {
|
||||
pub client_api_key_name: Option<String>,
|
||||
pub account_name: Option<String>,
|
||||
pub account_search: Option<String>,
|
||||
pub status: Option<String>,
|
||||
pub current_page: Option<u32>,
|
||||
pub page_size: Option<u16>,
|
||||
@@ -339,7 +339,7 @@ pub(crate) fn dashboard_today_range(
|
||||
pub(crate) fn usage_filter(query: &UsageQuery) -> domain::UsageFilter {
|
||||
domain::UsageFilter {
|
||||
client_api_key_name: non_empty(query.client_api_key_name.clone()),
|
||||
provider_account_name: non_empty(query.account_name.clone()),
|
||||
provider_account_search: non_empty(query.account_search.clone()),
|
||||
provider_kind: non_empty(query.provider.clone()),
|
||||
model_contains: non_empty(query.model.clone()),
|
||||
..Default::default()
|
||||
|
||||
@@ -19,7 +19,6 @@ use crate::openai::generation::{
|
||||
};
|
||||
|
||||
const OPENAI_SUBAGENT_KEY: &str = "x-openai-subagent";
|
||||
const CODEX_TURN_METADATA_KEY: &str = "x-codex-turn-metadata";
|
||||
const PASSTHROUGH_HEADERS_CONTEXT_KEY: &str = "opaque_request_headers";
|
||||
const DOWNSTREAM_WEBSOCKET_CONNECTION_ID_CONTEXT_KEY: &str = "downstream_websocket_connection_id";
|
||||
|
||||
@@ -36,9 +35,7 @@ pub(super) enum RequestDecodeSource {
|
||||
pub struct OpenAiRequestHeaders {
|
||||
turn_state: Option<String>,
|
||||
turn_metadata: Option<String>,
|
||||
beta_features: Option<String>,
|
||||
version: Option<String>,
|
||||
include_timing_metrics: Option<String>,
|
||||
codex_window_id: Option<String>,
|
||||
parent_thread_id: Option<String>,
|
||||
downstream_websocket_connection_id: Option<String>,
|
||||
@@ -62,9 +59,7 @@ impl OpenAiRequestHeaders {
|
||||
Self {
|
||||
turn_state: header_string(headers, "x-codex-turn-state"),
|
||||
turn_metadata: header_string(headers, "x-codex-turn-metadata"),
|
||||
beta_features: header_string(headers, "x-codex-beta-features"),
|
||||
version: header_string(headers, "version"),
|
||||
include_timing_metrics: header_string(headers, "x-responsesapi-include-timing-metrics"),
|
||||
codex_window_id: header_string(headers, "x-codex-window-id"),
|
||||
parent_thread_id: header_string(headers, "x-codex-parent-thread-id"),
|
||||
downstream_websocket_connection_id: None,
|
||||
@@ -120,22 +115,18 @@ impl OpenAiRequestHeaders {
|
||||
fn protocol_context(
|
||||
&self,
|
||||
use_websocket: Option<bool>,
|
||||
frame_turn_metadata: Option<&String>,
|
||||
source: RequestDecodeSource,
|
||||
) -> Map<String, Value> {
|
||||
let mut context = Map::new();
|
||||
insert_protocol_context(&mut context, "turn_state", self.turn_state.as_ref());
|
||||
insert_protocol_context(
|
||||
&mut context,
|
||||
"turn_metadata",
|
||||
frame_turn_metadata.or(self.turn_metadata.as_ref()),
|
||||
);
|
||||
insert_protocol_context(&mut context, "beta_features", self.beta_features.as_ref());
|
||||
insert_protocol_context(&mut context, "turn_metadata", self.turn_metadata.as_ref());
|
||||
if matches!(source, RequestDecodeSource::WebSocketFrame) {
|
||||
context.insert(
|
||||
gateway_protocol::openai::WEBSOCKET_FRAME_CONTEXT_KEY.to_owned(),
|
||||
Value::Bool(true),
|
||||
);
|
||||
}
|
||||
insert_protocol_context(&mut context, "version", self.version.as_ref());
|
||||
insert_protocol_context(
|
||||
&mut context,
|
||||
"include_timing_metrics",
|
||||
self.include_timing_metrics.as_ref(),
|
||||
);
|
||||
insert_protocol_context(
|
||||
&mut context,
|
||||
"codex_window_id",
|
||||
@@ -338,11 +329,7 @@ pub(super) fn decode_request_object(
|
||||
.unwrap_or(ContinuationIntent::None);
|
||||
|
||||
request_headers.apply_subagent(&mut object);
|
||||
let frame_turn_metadata = match source {
|
||||
RequestDecodeSource::Http => None,
|
||||
RequestDecodeSource::WebSocketFrame => frame_turn_metadata(&object),
|
||||
};
|
||||
let protocol_context = request_headers.protocol_context(use_websocket, frame_turn_metadata);
|
||||
let protocol_context = request_headers.protocol_context(use_websocket, source);
|
||||
|
||||
let semantics =
|
||||
gateway_protocol::openai::codex_responses_request_semantics(&object, &protocol_context);
|
||||
@@ -381,16 +368,6 @@ pub(super) fn decode_request_object(
|
||||
})
|
||||
}
|
||||
|
||||
fn frame_turn_metadata(body: &Map<String, Value>) -> Option<&String> {
|
||||
body.get("client_metadata")
|
||||
.and_then(Value::as_object)
|
||||
.and_then(|metadata| metadata.get(CODEX_TURN_METADATA_KEY))
|
||||
.and_then(|value| match value {
|
||||
Value::String(value) if !value.trim().is_empty() => Some(value),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
fn inject_subagent_metadata(body: &mut Map<String, Value>, subagent: &str) {
|
||||
let metadata = body
|
||||
.entry("client_metadata".to_owned())
|
||||
|
||||
@@ -74,13 +74,11 @@ pub(crate) async fn responses_websocket(
|
||||
&headers,
|
||||
connect_info.map(|Extension(ConnectInfo(address))| address),
|
||||
);
|
||||
let request_headers = OpenAiRequestHeaders::from_headers(&headers);
|
||||
ResponsesWebSocketAdapter::new(service).upgrade_with_client_context(
|
||||
websocket,
|
||||
client,
|
||||
client_ip,
|
||||
user_agent,
|
||||
request_headers,
|
||||
headers,
|
||||
middleware.map(|Extension(plan)| plan),
|
||||
lifetime.map(|Extension(lifetime)| lifetime),
|
||||
@@ -107,7 +105,6 @@ impl ResponsesWebSocketAdapter {
|
||||
client: AuthenticatedClient,
|
||||
client_ip: Option<IpAddr>,
|
||||
user_agent: Option<String>,
|
||||
request_headers: OpenAiRequestHeaders,
|
||||
raw_headers: HeaderMap,
|
||||
middleware: Option<FrozenMiddlewarePlan>,
|
||||
lifetime: Option<Arc<crate::middleware::CallLifetime>>,
|
||||
@@ -130,7 +127,6 @@ impl ResponsesWebSocketAdapter {
|
||||
connection_id,
|
||||
client_ip,
|
||||
user_agent,
|
||||
request_headers,
|
||||
raw_headers,
|
||||
lifecycle: self.service.lifecycle(),
|
||||
connection_guard,
|
||||
@@ -150,7 +146,6 @@ struct ResponsesWebSocketSession {
|
||||
connection_id: String,
|
||||
client_ip: Option<IpAddr>,
|
||||
user_agent: Option<String>,
|
||||
request_headers: OpenAiRequestHeaders,
|
||||
raw_headers: HeaderMap,
|
||||
lifecycle: Arc<dyn ConnectionLifecycle>,
|
||||
connection_guard: Box<dyn ConnectionGuard>,
|
||||
@@ -164,8 +159,7 @@ async fn serve_responses_websocket(socket: Socket, session: ResponsesWebSocketSe
|
||||
connection_id,
|
||||
client_ip,
|
||||
user_agent,
|
||||
request_headers,
|
||||
raw_headers,
|
||||
mut raw_headers,
|
||||
lifecycle,
|
||||
connection_guard,
|
||||
middleware,
|
||||
@@ -178,8 +172,6 @@ async fn serve_responses_websocket(socket: Socket, session: ResponsesWebSocketSe
|
||||
let cancellation = lifecycle.cancellation().child_token();
|
||||
let _connection_cancellation =
|
||||
crate::middleware::websocket::CancelOnDrop(Some(cancellation.clone()));
|
||||
let request_headers =
|
||||
request_headers.with_downstream_websocket_connection_id(connection_id.clone());
|
||||
// 帧中间件可能改写终态,此时不能据原始响应关闭下游连接
|
||||
let reconnect_for_replay = !middleware
|
||||
.as_ref()
|
||||
@@ -241,31 +233,32 @@ async fn serve_responses_websocket(socket: Socket, session: ResponsesWebSocketSe
|
||||
continue;
|
||||
}
|
||||
request_count = request_count.saturating_add(1);
|
||||
// 初步解码只保留路由事实,避免整份正文跨越准入等待和响应交付
|
||||
let model_hint = match decode_response_create_with_context(&payload, &request_headers) {
|
||||
Ok(decoded) => Some(decoded.metadata().requested_model().to_owned()),
|
||||
Err(error) => {
|
||||
trace_rejected_request(
|
||||
&correlation_id,
|
||||
connection.id(),
|
||||
&payload,
|
||||
"decode",
|
||||
&error.to_string(),
|
||||
);
|
||||
if send_protocol_error(
|
||||
&mut connection,
|
||||
StatusCode::BAD_REQUEST,
|
||||
error.protocol_body(),
|
||||
&correlation_id,
|
||||
)
|
||||
.await
|
||||
== ForwardOutcome::Disconnect
|
||||
{
|
||||
break;
|
||||
// 初步解码只取帧中的模型,实际执行使用该帧当前生效的请求头
|
||||
let model_hint =
|
||||
match decode_response_create_with_context(&payload, &OpenAiRequestHeaders::default()) {
|
||||
Ok(decoded) => Some(decoded.metadata().requested_model().to_owned()),
|
||||
Err(error) => {
|
||||
trace_rejected_request(
|
||||
&correlation_id,
|
||||
connection.id(),
|
||||
&payload,
|
||||
"decode",
|
||||
&error.to_string(),
|
||||
);
|
||||
if send_protocol_error(
|
||||
&mut connection,
|
||||
StatusCode::BAD_REQUEST,
|
||||
error.protocol_body(),
|
||||
&correlation_id,
|
||||
)
|
||||
.await
|
||||
== ForwardOutcome::Disconnect
|
||||
{
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
};
|
||||
};
|
||||
// deadline 与 Text 可能同时就绪;在任何上游执行开始前再次封住该竞争窗口
|
||||
if connection.is_expired() {
|
||||
trace_rejected_request(
|
||||
@@ -336,6 +329,9 @@ async fn serve_responses_websocket(socket: Socket, session: ResponsesWebSocketSe
|
||||
headers: encode_headers(&raw_headers),
|
||||
body: Bytes::from(payload),
|
||||
};
|
||||
// 握手的轮次头只属于首次执行,复用连接时由各 response.create 携带本轮数据
|
||||
raw_headers.remove("x-codex-turn-state");
|
||||
raw_headers.remove("x-codex-turn-metadata");
|
||||
let service_for_terminal = service.clone();
|
||||
let replay_for_terminal = replay.clone();
|
||||
let connection_id_for_terminal = connection.id().to_owned();
|
||||
|
||||
@@ -917,7 +917,7 @@ pub(super) fn usage_record_with_account(
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn usage_route_should_forward_name_and_model_contains_filters() {
|
||||
async fn usage_route_should_forward_record_text_filters() {
|
||||
use axum::{
|
||||
body::Body,
|
||||
http::{Request, StatusCode, header},
|
||||
@@ -933,7 +933,7 @@ async fn usage_route_should_forward_name_and_model_contains_filters() {
|
||||
.with_state(fixture.state())
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri("/api/admin/usage/records?model=%20coding%20&clientApiKeyName=%20Key%20%25%20&accountName=%20Account%20Name%20")
|
||||
.uri("/api/admin/usage/records?model=%20coding%20&clientApiKeyName=%20Key%20%25%20&accountSearch=%20alice%40%20")
|
||||
.header(header::COOKIE, "cpr_session=valid-session")
|
||||
.header("x-request-id", "req_usage_other_outcome")
|
||||
.body(Body::empty())
|
||||
@@ -947,10 +947,7 @@ async fn usage_route_should_forward_name_and_model_contains_filters() {
|
||||
let filter = &observations.records.last().expect("usage query").filter;
|
||||
assert_eq!(filter.client_api_key_name.as_deref(), Some("Key %"));
|
||||
assert_eq!(filter.model_contains.as_deref(), Some("coding"));
|
||||
assert_eq!(
|
||||
filter.provider_account_name.as_deref(),
|
||||
Some("Account Name")
|
||||
);
|
||||
assert_eq!(filter.provider_account_search.as_deref(), Some("alice@"));
|
||||
assert!(filter.client_api_key_ref.is_none());
|
||||
assert!(filter.provider_account_ref.is_none());
|
||||
}
|
||||
|
||||
@@ -45,6 +45,14 @@ impl Drop for TestServer {
|
||||
async fn connect(
|
||||
trace: Arc<AtomicFailureTrace>,
|
||||
response_headers: Vec<ProviderResponseHeader>,
|
||||
) -> (TestSocket, TestServer) {
|
||||
connect_with_headers(trace, response_headers, axum::http::HeaderMap::new()).await
|
||||
}
|
||||
|
||||
async fn connect_with_headers(
|
||||
trace: Arc<AtomicFailureTrace>,
|
||||
response_headers: Vec<ProviderResponseHeader>,
|
||||
opening_headers: axum::http::HeaderMap,
|
||||
) -> (TestSocket, TestServer) {
|
||||
let execution = Arc::new(AtomicFailureExecution {
|
||||
client: authenticated_client("sk_ws_atomic"),
|
||||
@@ -66,6 +74,7 @@ async fn connect(
|
||||
request
|
||||
.headers_mut()
|
||||
.insert(AUTHORIZATION, "Bearer sk_ws_atomic".parse().unwrap());
|
||||
request.headers_mut().extend(opening_headers);
|
||||
let (socket, response) = connect_async(request).await.expect("upgrade WebSocket");
|
||||
assert_eq!(response.status(), StatusCode::SWITCHING_PROTOCOLS);
|
||||
(socket, server)
|
||||
@@ -518,13 +527,32 @@ async fn errors_on_a_reused_connection_keep_their_own_upstream_ids() {
|
||||
initial_errors: Mutex::new(VecDeque::from(errors)),
|
||||
..AtomicFailureTrace::default()
|
||||
});
|
||||
let (mut socket, _server) = connect(trace, Vec::new()).await;
|
||||
let mut opening = axum::http::HeaderMap::new();
|
||||
opening.insert(
|
||||
"x-codex-turn-state",
|
||||
axum::http::HeaderValue::from_static("opening"),
|
||||
);
|
||||
opening.insert(
|
||||
"x-codex-turn-metadata",
|
||||
axum::http::HeaderValue::from_static(r#"{"request_kind":"turn"}"#),
|
||||
);
|
||||
let (mut socket, _server) = connect_with_headers(Arc::clone(&trace), Vec::new(), opening).await;
|
||||
for request_id in ["req_first", "req_second"] {
|
||||
send_request(&mut socket).await;
|
||||
let error = next_error(&mut socket).await;
|
||||
assert_eq!(error["headers"]["x-request-id"], request_id);
|
||||
assert_eq!(error["headers"]["x-gateway-request-id"], "req_ws_atomic");
|
||||
}
|
||||
assert_eq!(
|
||||
*trace.request_turn_headers.lock().unwrap(),
|
||||
[
|
||||
[
|
||||
Some("opening".to_owned()),
|
||||
Some(r#"{"request_kind":"turn"}"#.to_owned())
|
||||
],
|
||||
[None, None]
|
||||
]
|
||||
);
|
||||
assert_no_duplicate_failure(&mut socket).await;
|
||||
socket.close(None).await.unwrap();
|
||||
}
|
||||
|
||||
@@ -168,21 +168,31 @@ fn response_create_should_preserve_compaction_trigger_for_openai() {
|
||||
|
||||
#[test]
|
||||
fn response_create_should_prefer_frame_turn_metadata_over_opening_headers() {
|
||||
let frame = r#"{"request_kind":"turn","tool_namespaces_info":{"workspace":{}}}"#;
|
||||
let opening = r#"{"request_kind":"compaction"}"#;
|
||||
let decoded = decode_response_create_with_turn_header(
|
||||
json!({
|
||||
"type": "response.create",
|
||||
"model": "smart-code",
|
||||
"input": "hello",
|
||||
"client_metadata": {
|
||||
"x-codex-turn-metadata": r#"{"request_kind":"turn"}"#
|
||||
"x-codex-turn-metadata": frame
|
||||
}
|
||||
}),
|
||||
r#"{"request_kind":"compaction"}"#,
|
||||
opening,
|
||||
);
|
||||
let Operation::Generate(request) = decoded.operation() else {
|
||||
panic!("Responses must map to Generate");
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
request.protocol_payload().context()["turn_metadata"],
|
||||
opening
|
||||
);
|
||||
assert_eq!(
|
||||
request.protocol_payload().body()["client_metadata"]["x-codex-turn-metadata"],
|
||||
frame
|
||||
);
|
||||
let semantics = codex_responses_request_semantics(
|
||||
request.protocol_payload().body(),
|
||||
request.protocol_payload().context(),
|
||||
@@ -308,6 +318,7 @@ struct AtomicFailureTrace {
|
||||
committed: AtomicBool,
|
||||
finalized: AtomicBool,
|
||||
downstream_websocket_connection_ids: Mutex<Vec<String>>,
|
||||
request_turn_headers: Mutex<Vec<[Option<String>; 2]>>,
|
||||
initial_errors: Mutex<std::collections::VecDeque<EngineError>>,
|
||||
first_batch: Mutex<Option<CoordinatedEvent>>,
|
||||
session_drops: Arc<AtomicUsize>,
|
||||
@@ -437,6 +448,11 @@ impl ExecutionService for AtomicFailureExecution {
|
||||
.get("downstream_websocket_connection_id")
|
||||
.and_then(Value::as_str)
|
||||
{
|
||||
let context = generate.protocol_payload().context();
|
||||
self.trace.request_turn_headers.lock().unwrap().push(
|
||||
["turn_state", "turn_metadata"]
|
||||
.map(|key| context.get(key).and_then(Value::as_str).map(str::to_owned)),
|
||||
);
|
||||
self.trace
|
||||
.downstream_websocket_connection_ids
|
||||
.lock()
|
||||
|
||||
@@ -1947,7 +1947,7 @@ where
|
||||
let store = Arc::clone(self.engine.store());
|
||||
let request_id = self.request_id.clone();
|
||||
let persisted = self.request_persisted;
|
||||
// 错误、账号 lease 与决策 future 都跨 next_event 的取消保留
|
||||
// 错误、尝试记录与决策 future 都跨 next_event 的取消保留
|
||||
// 在持久化及策略回调完成前,不能再次拉流或建立新的 attempt
|
||||
self.current = Some(current);
|
||||
self.pending_stream_failure = Some(PendingStreamFailure {
|
||||
@@ -2440,6 +2440,9 @@ where
|
||||
}
|
||||
|
||||
async fn finish_interruption(&mut self, error: &EngineError) -> Result<(), EngineError> {
|
||||
if let Some(current) = self.current.as_mut() {
|
||||
current.stream.abort();
|
||||
}
|
||||
// 结算 I/O 可能继续等待,先冻结交付事实以保持终态与实际 HTTP 状态一致
|
||||
self.response_commit.finish();
|
||||
let (outcome, gateway_error) = match error {
|
||||
|
||||
@@ -250,8 +250,7 @@ const MAX_NATIVE_RESPONSE_EVENTS_PER_INPUT: usize = 64;
|
||||
/// 这样 Coordinator 可以先持久化 attempt,再越过发送屏障
|
||||
pub struct ProviderStream {
|
||||
metadata: ProviderCallMetadata,
|
||||
events: EventStream,
|
||||
_lease: Box<dyn ResourceLease>,
|
||||
source: Option<ProviderStreamSource>,
|
||||
native_response_translator: Option<Box<dyn NativeResponseTranslator>>,
|
||||
account_feedback: Option<ProviderStreamAccountFeedback>,
|
||||
account_protection: Option<super::account_protection::AttemptProtection>,
|
||||
@@ -259,7 +258,12 @@ pub struct ProviderStream {
|
||||
Option<futures::future::BoxFuture<'static, Option<Result<ProviderEvent, ProviderError>>>>,
|
||||
validator: Result<EventSequenceValidator, EventSequenceError>,
|
||||
strict_canonical_seen: bool,
|
||||
terminated: bool,
|
||||
}
|
||||
|
||||
// 事件流内部也可能持有同一租约,结束读取时须一并丢弃
|
||||
struct ProviderStreamSource {
|
||||
events: EventStream,
|
||||
_lease: Box<dyn ResourceLease>,
|
||||
}
|
||||
|
||||
struct ProviderStreamAccountFeedback {
|
||||
@@ -360,18 +364,25 @@ impl ProviderStream {
|
||||
{
|
||||
Self {
|
||||
metadata,
|
||||
events: Box::pin(events),
|
||||
_lease: Box::new(lease),
|
||||
source: Some(ProviderStreamSource {
|
||||
events: Box::pin(events),
|
||||
_lease: Box::new(lease),
|
||||
}),
|
||||
native_response_translator: None,
|
||||
account_feedback: None,
|
||||
account_protection: None,
|
||||
protection_feedback: None,
|
||||
validator: Ok(EventSequenceValidator::new()),
|
||||
strict_canonical_seen: false,
|
||||
terminated: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn abort(&mut self) {
|
||||
self.source.take();
|
||||
self.account_protection.take();
|
||||
self.protection_feedback.take();
|
||||
}
|
||||
|
||||
/// 在第一次发送前捕获反馈栅栏,诊断调用由协调器排除
|
||||
#[must_use]
|
||||
pub async fn with_account_error_protection(
|
||||
@@ -425,6 +436,15 @@ impl ProviderStream {
|
||||
/// source/target 格式不一致、单事件展开越界、终态被丢弃或 Provider 转换失败时
|
||||
/// 返回错误
|
||||
pub fn translate_native_response(
|
||||
&mut self,
|
||||
event: ProviderEvent,
|
||||
terminal: bool,
|
||||
) -> Result<Vec<ProviderEvent>, ProviderError> {
|
||||
self.translate_event(event, terminal)
|
||||
.inspect_err(|_| self.abort())
|
||||
}
|
||||
|
||||
fn translate_event(
|
||||
&mut self,
|
||||
mut event: ProviderEvent,
|
||||
terminal: bool,
|
||||
@@ -979,8 +999,12 @@ impl Stream for ProviderStream {
|
||||
};
|
||||
}
|
||||
let result = this.poll_event(context);
|
||||
let upstream_finished = matches!(&result, Poll::Ready(Some(Err(_)) | None));
|
||||
if upstream_finished {
|
||||
this.source.take();
|
||||
}
|
||||
// 客户端可在成功终态后停止拉取,不能依赖之后的 EOF 才清除保护证据
|
||||
let protection_terminal = matches!(&result, Poll::Ready(Some(Err(_)) | None))
|
||||
let protection_terminal = upstream_finished
|
||||
|| (matches!(&result, Poll::Ready(Some(Ok(_))))
|
||||
&& this
|
||||
.account_protection
|
||||
@@ -1003,14 +1027,14 @@ impl ProviderStream {
|
||||
context: &mut Context<'_>,
|
||||
) -> Poll<Option<Result<ProviderEvent, ProviderError>>> {
|
||||
let this = self;
|
||||
if this.terminated {
|
||||
let Some(source) = this.source.as_mut() else {
|
||||
return Poll::Ready(None);
|
||||
}
|
||||
};
|
||||
if let Some(feedback) = this.account_feedback.as_mut() {
|
||||
feedback.start();
|
||||
}
|
||||
|
||||
match this.events.as_mut().poll_next(context) {
|
||||
match source.events.as_mut().poll_next(context) {
|
||||
Poll::Pending => Poll::Pending,
|
||||
Poll::Ready(Some(Ok(event))) => {
|
||||
// 纯 wire 流的 facts 仍只是旁路观测;一旦交付 canonical-only
|
||||
@@ -1025,7 +1049,6 @@ impl ProviderStream {
|
||||
}
|
||||
}
|
||||
if this.strict_canonical_seen && this.validator.is_err() {
|
||||
this.terminated = true;
|
||||
let error =
|
||||
ProviderError::new(ProviderErrorKind::Protocol, UpstreamSendState::Sent);
|
||||
if let Some(feedback) = this.account_feedback.as_mut() {
|
||||
@@ -1042,14 +1065,12 @@ impl ProviderStream {
|
||||
Poll::Ready(Some(Ok(event)))
|
||||
}
|
||||
Poll::Ready(Some(Err(error))) => {
|
||||
this.terminated = true;
|
||||
if let Some(feedback) = this.account_feedback.as_mut() {
|
||||
feedback.report_failure(&error);
|
||||
}
|
||||
Poll::Ready(Some(Err(error)))
|
||||
}
|
||||
Poll::Ready(None) => {
|
||||
this.terminated = true;
|
||||
let validation = if this.strict_canonical_seen {
|
||||
this.validator
|
||||
.as_ref()
|
||||
|
||||
@@ -299,14 +299,17 @@ fn complete_success_is_reported_once_but_unfinished_drop_is_not_success() {
|
||||
Ok(gateway_core::event::GatewayEvent::Started(response.clone()).into()),
|
||||
Ok(gateway_core::event::GatewayEvent::Completed(response).into()),
|
||||
]);
|
||||
let mut completed = ProviderStream::new(metadata(), events, ())
|
||||
let released = Arc::new(AtomicUsize::new(0));
|
||||
let mut completed = ProviderStream::new(metadata(), events, Lease(released.clone()))
|
||||
.with_account_error_protection(store.clone(), Arc::new(policy().compile()), 5)
|
||||
.await;
|
||||
assert!(completed.next().await.unwrap().is_ok());
|
||||
assert_eq!(store.successes.load(Ordering::SeqCst), 0);
|
||||
assert!(completed.next().await.unwrap().is_ok());
|
||||
assert_eq!(store.successes.load(Ordering::SeqCst), 1);
|
||||
assert_eq!(released.load(Ordering::SeqCst), 0);
|
||||
assert!(completed.next().await.is_none());
|
||||
assert_eq!(released.load(Ordering::SeqCst), 1);
|
||||
assert!(completed.next().await.is_none());
|
||||
assert_eq!(store.successes.load(Ordering::SeqCst), 1);
|
||||
});
|
||||
@@ -351,27 +354,27 @@ fn stalled_begin_and_feedback_are_bounded_and_preserve_original_failure() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cancelling_pending_feedback_releases_lease_and_does_not_report_success() {
|
||||
fn failed_stream_releases_lease_while_protection_feedback_is_pending() {
|
||||
futures::executor::block_on(async {
|
||||
let store = Arc::new(Feedback {
|
||||
stalled_feedback: true,
|
||||
..Feedback::default()
|
||||
});
|
||||
let released = Arc::new(AtomicUsize::new(0));
|
||||
let mut stream = ProviderStream::new(
|
||||
metadata(),
|
||||
stream::iter([Err(failure("invalid_prompt"))]),
|
||||
Lease(released.clone()),
|
||||
)
|
||||
.with_account_error_protection(store.clone(), Arc::new(policy().compile()), 5)
|
||||
.await;
|
||||
let lease = Arc::new(Lease(released.clone()));
|
||||
let events = stream::unfold(Arc::clone(&lease), |lease| async move {
|
||||
Some((Err(failure("invalid_prompt")), lease))
|
||||
});
|
||||
let mut stream = ProviderStream::new(metadata(), events, lease)
|
||||
.with_account_error_protection(store.clone(), Arc::new(policy().compile()), 5)
|
||||
.await;
|
||||
assert!(
|
||||
timeout(Duration::from_millis(10), stream.next())
|
||||
.await
|
||||
.is_err()
|
||||
);
|
||||
drop(stream);
|
||||
assert_eq!(released.load(Ordering::SeqCst), 1);
|
||||
drop(stream);
|
||||
assert_eq!(store.successes.load(Ordering::SeqCst), 0);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -898,7 +898,7 @@ impl Provider for ScriptedProvider {
|
||||
futures::stream::iter(items.into_iter().map(canonical_provider_event))
|
||||
.chain(futures::stream::pending()),
|
||||
),
|
||||
(),
|
||||
TrackedLease(Arc::clone(&self.released_leases)),
|
||||
))
|
||||
}
|
||||
Script::ObservedStream { account_id, items } => {
|
||||
@@ -3402,7 +3402,16 @@ fn recovered_credential_retries_the_same_account_exactly_once() {
|
||||
CancellationToken::new(),
|
||||
))
|
||||
.expect("start execution");
|
||||
block_on(session.collect_uncommitted()).expect("recovered account succeeds");
|
||||
let (release, gate) = oneshot::channel();
|
||||
*store.intermediate_gate.lock().unwrap() = Some(gate);
|
||||
block_on(async {
|
||||
let mut collect = Box::pin(session.collect_uncommitted());
|
||||
assert!(futures::poll!(collect.as_mut()).is_pending());
|
||||
assert_eq!(provider.contexts.lock().unwrap().len(), 1);
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 1);
|
||||
release.send(()).unwrap();
|
||||
collect.await.expect("recovered account succeeds");
|
||||
});
|
||||
block_on(session.commit_downstream(Some(200))).expect("commit response");
|
||||
|
||||
let contexts = provider.contexts.lock().expect("contexts lock");
|
||||
@@ -5408,7 +5417,7 @@ fn delivered_prelude_allows_retry_without_control_until_non_replayable_delivery(
|
||||
fn cancellation_before_pending_delivery_commit_reaches_terminal_state() {
|
||||
let operation = generate_operation();
|
||||
let route_plan = plan(&operation);
|
||||
let (coordinator, store, _) = coordinator(vec![Script::Stream {
|
||||
let (coordinator, store, provider) = coordinator(vec![Script::Stream {
|
||||
account_id: "acct_first",
|
||||
items: complete_stream(None),
|
||||
}]);
|
||||
@@ -5429,7 +5438,15 @@ fn cancellation_before_pending_delivery_commit_reaches_terminal_state() {
|
||||
CommitRequirement::CommitBeforeDelivery
|
||||
);
|
||||
|
||||
block_on(session.cancel_and_finalize()).expect("cancel finalization");
|
||||
let (release, gate) = oneshot::channel();
|
||||
*store.send_gate.lock().unwrap() = Some(gate);
|
||||
block_on(async {
|
||||
let mut cancel = Box::pin(session.cancel_and_finalize());
|
||||
assert!(futures::poll!(cancel.as_mut()).is_pending());
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 1);
|
||||
release.send(()).unwrap();
|
||||
cancel.await.expect("cancel finalization");
|
||||
});
|
||||
|
||||
assert!(session.is_finalized());
|
||||
let state = store.state.lock().expect("store lock");
|
||||
@@ -5946,7 +5963,7 @@ fn interrupted_stream_failure_preserves_the_original_attempt_and_retry_decision(
|
||||
let mut resumed = Box::pin(session.next_event());
|
||||
assert!(futures::poll!(resumed.as_mut()).is_pending());
|
||||
assert_eq!(provider.contexts.lock().unwrap().len(), 1);
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 0);
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 1);
|
||||
release
|
||||
.send(())
|
||||
.expect("the original write still owns its receiver");
|
||||
@@ -6144,6 +6161,7 @@ fn charged_terminal_write_resumes_without_replacing_success_failure_or_send_fact
|
||||
assert!(futures::poll!(collect.as_mut()).is_pending());
|
||||
} else {
|
||||
session.collect_uncommitted().await.unwrap();
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 1);
|
||||
let mut commit = Box::pin(session.commit_downstream(Some(200)));
|
||||
assert!(futures::poll!(commit.as_mut()).is_pending());
|
||||
}
|
||||
@@ -6187,7 +6205,7 @@ fn charged_terminal_write_resumes_without_replacing_success_failure_or_send_fact
|
||||
fn deadline_after_commit_is_persisted_as_incomplete() {
|
||||
let operation = generate_operation();
|
||||
let route_plan = plan(&operation);
|
||||
let (coordinator, store, _) = coordinator(vec![Script::HangingStream {
|
||||
let (coordinator, store, provider) = coordinator(vec![Script::HangingStream {
|
||||
account_id: "acct_first",
|
||||
items: vec![Ok(GatewayEvent::Started(ResponseMeta::new(
|
||||
"response-1",
|
||||
@@ -6215,6 +6233,7 @@ fn deadline_after_commit_is_persisted_as_incomplete() {
|
||||
let error = block_on(session.next_event()).expect_err("deadline elapses mid-stream");
|
||||
|
||||
assert!(matches!(error, EngineError::Deadline));
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 1);
|
||||
let state = store.state.lock().expect("store lock");
|
||||
assert_eq!(state.finalizations[0].outcome, ExecutionOutcome::Incomplete);
|
||||
assert!(state.finalizations[0].committed);
|
||||
@@ -6491,8 +6510,6 @@ fn connection_recovery_rotates_only_after_retrying_the_first_account() {
|
||||
assert_eq!(context.transport(), AttemptTransport::Fallback);
|
||||
}
|
||||
assert_eq!(store.state.lock().unwrap().intermediate_failures, 2);
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 2);
|
||||
drop(session);
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 3);
|
||||
}
|
||||
|
||||
@@ -6556,8 +6573,6 @@ fn connection_recovery_keeps_one_account_and_http_transport_until_success() {
|
||||
let state = store.state.lock().unwrap();
|
||||
assert_eq!(state.intermediate_failures, 3);
|
||||
assert_eq!(state.finalizations[0].outcome, ExecutionOutcome::Succeeded);
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 3);
|
||||
drop(session);
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 4);
|
||||
},
|
||||
)));
|
||||
@@ -6654,8 +6669,6 @@ fn connection_recovery_does_not_delay_existing_account_rotation() {
|
||||
let state = store.state.lock().unwrap();
|
||||
assert_eq!(state.intermediate_failures, 2);
|
||||
assert_eq!(state.finalizations[0].outcome, ExecutionOutcome::Succeeded);
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 2);
|
||||
drop(session);
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 3);
|
||||
}
|
||||
|
||||
@@ -6749,8 +6762,6 @@ fn connection_recovery_exhaustion_preserves_transport_failure() {
|
||||
store.state.lock().unwrap().finalizations[0].attempt_count,
|
||||
4
|
||||
);
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 3);
|
||||
drop(session);
|
||||
assert_eq!(provider.released_leases.load(Ordering::SeqCst), 4);
|
||||
}
|
||||
|
||||
|
||||
@@ -1955,6 +1955,7 @@ impl NativeResponseTranslator for FailingNativeResponseTranslator {
|
||||
|
||||
struct FailingNativeResponseProvider {
|
||||
calls: Arc<AtomicUsize>,
|
||||
lease: Arc<()>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
@@ -2010,7 +2011,7 @@ impl Provider for FailingNativeResponseProvider {
|
||||
GatewayEvent::Completed(response),
|
||||
)),
|
||||
]),
|
||||
(),
|
||||
Arc::clone(&self.lease),
|
||||
)
|
||||
.with_native_response_translator(FailingNativeResponseTranslator::default()))
|
||||
}
|
||||
@@ -2020,8 +2021,10 @@ impl Provider for FailingNativeResponseProvider {
|
||||
fn native_response_failure_after_downstream_commit_is_not_replayed() {
|
||||
block_on(async {
|
||||
let calls = Arc::new(AtomicUsize::new(0));
|
||||
let lease = Arc::new(());
|
||||
let provider_index = ProviderRegistry::new([Arc::new(FailingNativeResponseProvider {
|
||||
calls: Arc::clone(&calls),
|
||||
lease: Arc::clone(&lease),
|
||||
}) as Arc<dyn Provider>])
|
||||
.unwrap();
|
||||
let generation = super::extensions::reference("native-response-failure");
|
||||
@@ -2054,6 +2057,7 @@ fn native_response_failure_after_downstream_commit_is_not_replayed() {
|
||||
}));
|
||||
started.session.commit_downstream(Some(200)).await.unwrap();
|
||||
|
||||
let active_refs = Arc::strong_count(&lease);
|
||||
let error = started.session.next_event().await.unwrap_err();
|
||||
assert!(matches!(
|
||||
error,
|
||||
@@ -2061,6 +2065,7 @@ fn native_response_failure_after_downstream_commit_is_not_replayed() {
|
||||
if error.kind() == ProviderErrorKind::Protocol
|
||||
&& error.send_state() == UpstreamSendState::Sent
|
||||
));
|
||||
assert_eq!(Arc::strong_count(&lease), active_refs - 1);
|
||||
assert_eq!(calls.load(Ordering::SeqCst), 1);
|
||||
started.session.detach_finalize().await;
|
||||
});
|
||||
|
||||
@@ -6,6 +6,38 @@ const MULTI_AGENT_MODE_OPEN_TAG: &str = "<multi_agent_mode>";
|
||||
const MULTI_AGENT_MODE_CLOSE_TAG: &str = "</multi_agent_mode>";
|
||||
const PROACTIVE_MULTI_AGENT_MODE_PREFIX: &str = "Proactive multi-agent delegation is active.";
|
||||
|
||||
/// 标记实际入站的 Responses WS 帧,独立于出站传输偏好
|
||||
pub const WEBSOCKET_FRAME_CONTEXT_KEY: &str = "websocket_frame";
|
||||
|
||||
/// 当前帧携带的完整元数据仅用于解释正文,握手或 HTTP 头保留各自的出站值
|
||||
#[must_use]
|
||||
pub fn observed_turn_metadata<'a>(
|
||||
body: &'a Map<String, Value>,
|
||||
header: Option<&'a str>,
|
||||
websocket_frame: bool,
|
||||
) -> Option<&'a str> {
|
||||
if websocket_frame && let Some(frame) = client_metadata_string(body, "x-codex-turn-metadata") {
|
||||
return Some(frame);
|
||||
}
|
||||
header
|
||||
}
|
||||
|
||||
/// HTTP 请求使用本次请求头,WS 帧使用本次正文或首次握手头
|
||||
#[must_use]
|
||||
pub fn context_turn_metadata<'a>(
|
||||
body: &'a Map<String, Value>,
|
||||
context: &'a Map<String, Value>,
|
||||
) -> Option<&'a str> {
|
||||
observed_turn_metadata(
|
||||
body,
|
||||
context.get("turn_metadata").and_then(Value::as_str),
|
||||
context
|
||||
.get(WEBSOCKET_FRAME_CONTEXT_KEY)
|
||||
.and_then(Value::as_bool)
|
||||
== Some(true),
|
||||
)
|
||||
}
|
||||
|
||||
/// 各 Codex 端点共用的显式根会话身份
|
||||
/// 连接边界的 session_id 优先,
|
||||
/// 正文及官方 metadata 只做回退;不从 turn、window 或请求内容猜测归属
|
||||
@@ -24,9 +56,7 @@ pub fn codex_account_session_id(
|
||||
client_metadata_string(body, "session_id")
|
||||
.map(str::to_owned)
|
||||
.or_else(|| request_turn_metadata(body).and_then(turn_metadata_session_id))
|
||||
.or_else(|| {
|
||||
non_empty_string(context.get("turn_metadata")).and_then(turn_metadata_session_id)
|
||||
})
|
||||
.or_else(|| context_turn_metadata(body, context).and_then(turn_metadata_session_id))
|
||||
.or_else(|| non_empty_string(body.get("session_id")).map(str::to_owned))
|
||||
.or_else(|| codex_session_id(body, context))
|
||||
}
|
||||
@@ -40,9 +70,7 @@ pub fn codex_account_thread_id(
|
||||
client_metadata_string(body, "thread_id")
|
||||
.map(str::to_owned)
|
||||
.or_else(|| request_turn_metadata(body).and_then(turn_metadata_thread_id))
|
||||
.or_else(|| {
|
||||
non_empty_string(context.get("turn_metadata")).and_then(turn_metadata_thread_id)
|
||||
})
|
||||
.or_else(|| context_turn_metadata(body, context).and_then(turn_metadata_thread_id))
|
||||
.or_else(|| codex_thread_id(body, context))
|
||||
}
|
||||
|
||||
@@ -64,7 +92,7 @@ pub fn codex_turn_id(body: &Map<String, Value>, context: &Map<String, Value>) ->
|
||||
client_metadata_string(body, "turn_id")
|
||||
.map(str::to_owned)
|
||||
.or_else(|| request_turn_metadata(body).and_then(turn_metadata_turn_id))
|
||||
.or_else(|| non_empty_string(context.get("turn_metadata")).and_then(turn_metadata_turn_id))
|
||||
.or_else(|| context_turn_metadata(body, context).and_then(turn_metadata_turn_id))
|
||||
.or_else(|| codex_identity_field(body, context, "turn_id"))
|
||||
}
|
||||
|
||||
@@ -90,7 +118,8 @@ fn codex_identity_field(
|
||||
.or_else(|| client_metadata_string(body, field))
|
||||
.map(str::to_owned)
|
||||
.or_else(|| {
|
||||
let metadata = non_empty_string(context.get("turn_metadata"))
|
||||
let metadata = context_turn_metadata(body, context)
|
||||
.filter(|value| !value.trim().is_empty())
|
||||
.or_else(|| request_turn_metadata(body))?;
|
||||
let metadata: Value = serde_json::from_str(metadata).ok()?;
|
||||
non_empty_string(metadata.get(field)).map(str::to_owned)
|
||||
@@ -127,7 +156,7 @@ pub fn codex_responses_request_semantics(
|
||||
) -> CodexResponsesRequestSemantics {
|
||||
codex_responses_request_semantics_with_turn_metadata(
|
||||
body,
|
||||
context.get("turn_metadata").and_then(Value::as_str),
|
||||
context_turn_metadata(body, context),
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -21,9 +21,10 @@ mod headers;
|
||||
pub mod sse;
|
||||
|
||||
pub use codex::{
|
||||
CodexResponsesRequestSemantics, codex_account_session_id, codex_account_thread_id,
|
||||
codex_responses_request_semantics, codex_responses_request_semantics_with_turn_metadata,
|
||||
codex_session_id, codex_thread_id, codex_turn_id, turn_metadata_session_id,
|
||||
CodexResponsesRequestSemantics, WEBSOCKET_FRAME_CONTEXT_KEY, codex_account_session_id,
|
||||
codex_account_thread_id, codex_responses_request_semantics,
|
||||
codex_responses_request_semantics_with_turn_metadata, codex_session_id, codex_thread_id,
|
||||
codex_turn_id, context_turn_metadata, observed_turn_metadata, turn_metadata_session_id,
|
||||
turn_metadata_thread_id, turn_metadata_turn_id,
|
||||
};
|
||||
pub use headers::{
|
||||
|
||||
@@ -32,7 +32,7 @@ pub fn request_observation(
|
||||
OPENAI_PROTOCOL => {
|
||||
let semantics = codex_responses_request_semantics_with_turn_metadata(
|
||||
body,
|
||||
context.get("turn_metadata").and_then(Value::as_str),
|
||||
crate::openai::context_turn_metadata(body, context),
|
||||
previous_reasoning_effort_override,
|
||||
);
|
||||
RequestObservation {
|
||||
|
||||
@@ -36,10 +36,8 @@ impl AnalyticsSnapshot<'_> {
|
||||
|| filter.attempt_index.is_some()
|
||||
|| filter.status_code.is_some()
|
||||
|| filter.outcome.is_some()
|
||||
|| filter.search.is_some()
|
||||
|| filter.model_contains.is_some()
|
||||
|| filter.client_api_key_name.is_some()
|
||||
|| filter.provider_account_name.is_some()
|
||||
|| filter.needs_observation_text()
|
||||
|| matches!(
|
||||
dimension,
|
||||
Some(DiagnosticDimension::Failure | DiagnosticDimension::Status)
|
||||
|
||||
@@ -1232,10 +1232,8 @@ pub(super) fn collection_for(
|
||||
|| filter.attempt_index.is_some()
|
||||
|| filter.response_id.is_some()
|
||||
|| filter.upstream_request_id.is_some()
|
||||
|| filter.search.is_some()
|
||||
|| filter.model_contains.is_some()
|
||||
|| filter.client_api_key_name.is_some()
|
||||
|| filter.provider_account_name.is_some()
|
||||
|| filter.needs_observation_text()
|
||||
{
|
||||
return None;
|
||||
}
|
||||
@@ -1286,21 +1284,15 @@ pub(crate) fn push_analytics_filter(
|
||||
alias: &str,
|
||||
) {
|
||||
let mut typed = filter.clone();
|
||||
typed.search = None;
|
||||
typed.client_api_key_name = None;
|
||||
typed.provider_account_name = None;
|
||||
let text = UsageRecordFilter {
|
||||
client_api_key_name: typed.client_api_key_name.take(),
|
||||
provider_account_search: typed.provider_account_search.take(),
|
||||
search: typed.search.take(),
|
||||
..Default::default()
|
||||
};
|
||||
push_usage_filter(query, &typed, alias);
|
||||
if filter.search.is_some()
|
||||
|| filter.client_api_key_name.is_some()
|
||||
|| filter.provider_account_name.is_some()
|
||||
{
|
||||
if text.needs_observation_text() {
|
||||
query.push(format!(" and exists(select 1 from model_request_observations searched where searched.id={alias}.id"));
|
||||
let text = UsageRecordFilter {
|
||||
client_api_key_name: filter.client_api_key_name.clone(),
|
||||
provider_account_name: filter.provider_account_name.clone(),
|
||||
search: filter.search.clone(),
|
||||
..Default::default()
|
||||
};
|
||||
push_usage_filter(query, &text, "searched");
|
||||
query.push(")");
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ pub(crate) fn store_usage_filter(filter: admin_observability::UsageFilter) -> Us
|
||||
UsageRecordFilter {
|
||||
model_contains: filter.model_contains,
|
||||
client_api_key_name: filter.client_api_key_name,
|
||||
provider_account_name: filter.provider_account_name,
|
||||
provider_account_search: filter.provider_account_search,
|
||||
client_api_key_ref: filter.client_api_key_ref,
|
||||
request_id: filter.request_id,
|
||||
provider_account_ref: filter.provider_account_ref,
|
||||
|
||||
@@ -40,7 +40,7 @@ pub(crate) fn observability_page_offset(
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq)]
|
||||
pub struct UsageRecordFilter {
|
||||
pub client_api_key_name: Option<String>,
|
||||
pub provider_account_name: Option<String>,
|
||||
pub provider_account_search: Option<String>,
|
||||
pub client_api_key_ref: Option<String>,
|
||||
pub request_id: Option<String>,
|
||||
pub provider_account_ref: Option<String>,
|
||||
@@ -58,6 +58,12 @@ pub struct UsageRecordFilter {
|
||||
}
|
||||
|
||||
impl UsageRecordFilter {
|
||||
pub(super) fn needs_observation_text(&self) -> bool {
|
||||
self.search.is_some()
|
||||
|| self.client_api_key_name.is_some()
|
||||
|| self.provider_account_search.is_some()
|
||||
}
|
||||
|
||||
pub fn validate(&self) -> StoreResult<()> {
|
||||
for (value, field) in [
|
||||
(
|
||||
@@ -65,8 +71,8 @@ impl UsageRecordFilter {
|
||||
"client API key name filter",
|
||||
),
|
||||
(
|
||||
self.provider_account_name.as_deref(),
|
||||
"provider account name filter",
|
||||
self.provider_account_search.as_deref(),
|
||||
"provider account search filter",
|
||||
),
|
||||
(self.client_api_key_ref.as_deref(), "client API key filter"),
|
||||
(self.request_id.as_deref(), "request ID filter"),
|
||||
|
||||
@@ -11,18 +11,10 @@ pub(crate) fn push_usage_filter(
|
||||
filter: &UsageRecordFilter,
|
||||
alias: &str,
|
||||
) {
|
||||
for (column, value) in [
|
||||
("client_api_key_name_snapshot", &filter.client_api_key_name),
|
||||
(
|
||||
"provider_account_name_snapshot",
|
||||
&filter.provider_account_name,
|
||||
),
|
||||
] {
|
||||
if let Some(value) = value {
|
||||
query.push(format!(" and {alias}.{column} like "));
|
||||
query.push_bind(literal_contains_pattern(value));
|
||||
query.push(" escape '\\'");
|
||||
}
|
||||
if let Some(value) = &filter.client_api_key_name {
|
||||
query.push(format!(" and {alias}.client_api_key_name_snapshot like "));
|
||||
query.push_bind(literal_contains_pattern(value));
|
||||
query.push(" escape '\\'");
|
||||
}
|
||||
if let Some(value) = &filter.client_api_key_ref {
|
||||
query.push(format!(" and {alias}.client_api_key_ref = "));
|
||||
@@ -51,11 +43,23 @@ pub(crate) fn push_usage_filter(
|
||||
query.push_bind(value.clone());
|
||||
query.push(")");
|
||||
}
|
||||
if let Some(value) = &filter.model_contains {
|
||||
for (value, first, second) in [
|
||||
(
|
||||
&filter.provider_account_search,
|
||||
"provider_account_name_snapshot",
|
||||
"provider_account_email_snapshot",
|
||||
),
|
||||
(
|
||||
&filter.model_contains,
|
||||
"requested_model_id",
|
||||
"upstream_model_id",
|
||||
),
|
||||
] {
|
||||
let Some(value) = value else { continue };
|
||||
let pattern = literal_contains_pattern(value);
|
||||
query.push(format!(" and ({alias}.requested_model_id like "));
|
||||
query.push(format!(" and ({alias}.{first} like "));
|
||||
query.push_bind(pattern.clone());
|
||||
query.push(format!(" escape '\\' or {alias}.upstream_model_id like "));
|
||||
query.push(format!(" escape '\\' or {alias}.{second} like "));
|
||||
query.push_bind(pattern);
|
||||
query.push(" escape '\\')");
|
||||
}
|
||||
|
||||
@@ -961,6 +961,69 @@ async fn usage_search_should_ignore_request_ids_and_preserve_exact_filters() {
|
||||
database.close().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn usage_account_filter_matches_only_historical_names_and_emails() {
|
||||
use admin_observability::UsageRecordStatus::{All, Error, Success};
|
||||
|
||||
let Some(database) = TestDatabase::create("usage_account_filter").await else {
|
||||
return;
|
||||
};
|
||||
let now = Utc::now();
|
||||
seed_observability_facts(&database.pool, now)
|
||||
.await
|
||||
.expect("seed observability facts");
|
||||
seed_calculated_billing_facts(&database.pool, now)
|
||||
.await
|
||||
.expect("seed unrelated request");
|
||||
sqlx::query(
|
||||
"update model_requests set request_observation_json = jsonb_set(
|
||||
request_observation_json, '{account}', case when id = 'req_observe_success'
|
||||
then jsonb_build_object('email', $1::text)
|
||||
else jsonb_build_object('name', $2::text) end)
|
||||
where id in ('req_observe_success', 'req_observe_failed')",
|
||||
)
|
||||
.bind(r"alice_%\ops@example.invalid")
|
||||
.bind("alice team")
|
||||
.execute(&database.pool)
|
||||
.await
|
||||
.expect("set email-only and name-only snapshots");
|
||||
super::drain_analytics(&database.pool).await;
|
||||
|
||||
let range = ObservabilityRange::new(now - TimeDelta::hours(1), now).expect("range");
|
||||
let repo = observability_repository(&database.pool);
|
||||
for (status, search, page, total, expected_id) in [
|
||||
(Success, r"_%\", 1, 1, Some("req_observe_success")),
|
||||
(Error, "team", 1, 1, Some("req_observe_failed")),
|
||||
(All, "alice", 1, 2, Some("req_observe_failed")),
|
||||
(All, "alice", 2, 2, Some("req_observe_success")),
|
||||
(Error, "@example", 1, 0, None),
|
||||
(All, "ALICE", 1, 0, None),
|
||||
(All, "alice%ops", 1, 0, None),
|
||||
(All, "public-model", 1, 0, None),
|
||||
] {
|
||||
let result = repo
|
||||
.list_usage_records(UsageRecordQuery {
|
||||
status,
|
||||
range,
|
||||
filter: UsageRecordFilter {
|
||||
provider_account_search: Some(search.to_owned()),
|
||||
..Default::default()
|
||||
},
|
||||
current_page: page,
|
||||
page_size: ObservabilityPageSize::new(1).expect("page size"),
|
||||
})
|
||||
.await
|
||||
.expect("filter usage by account snapshot");
|
||||
assert_eq!(result.total, total, "{status:?} {search} page {page}");
|
||||
assert_eq!(
|
||||
result.items.first().map(|item| item.id.as_str()),
|
||||
expected_id,
|
||||
"{status:?} {search} page {page}"
|
||||
);
|
||||
}
|
||||
database.close().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn usage_search_should_match_account_email_and_name_substrings() {
|
||||
let Some(database) = TestDatabase::create("usage_account_contains").await else {
|
||||
@@ -2207,7 +2270,7 @@ async fn admin_observability_adapter_preserves_utc_queries_metrics_costs_and_det
|
||||
filter: admin_observability::UsageFilter {
|
||||
model_contains: None,
|
||||
client_api_key_name: None,
|
||||
provider_account_name: None,
|
||||
provider_account_search: Some("@example.invalid".to_owned()),
|
||||
client_api_key_ref: Some("key_observe".to_owned()),
|
||||
request_id: Some("req_observe_success".to_owned()),
|
||||
provider_account_ref: Some("acct_observe".to_owned()),
|
||||
|
||||
@@ -1452,13 +1452,14 @@ async fn queued_history_keeps_frozen_identity_after_configuration_deletion() {
|
||||
if published {
|
||||
super::drain_analytics(&database.pool).await;
|
||||
}
|
||||
for (key_name, account_name, expected) in [
|
||||
for (key_name, account_search, expected) in [
|
||||
(Some("Original Key"), None, 1),
|
||||
(None, Some("Original Account"), 1),
|
||||
(Some("Original Key"), Some("Original Account"), 1),
|
||||
(Some("Original"), None, 1),
|
||||
(Some("ginal K"), None, 1),
|
||||
(None, Some("ginal Acc"), 1),
|
||||
(None, Some("old@"), 1),
|
||||
(Some("queued_key"), None, 0),
|
||||
(None, Some("queued_account"), 0),
|
||||
(Some("Original Account"), None, 0),
|
||||
@@ -1473,7 +1474,7 @@ async fn queued_history_keeps_frozen_identity_after_configuration_deletion() {
|
||||
started_at,
|
||||
UsageRecordFilter {
|
||||
client_api_key_name: key_name.map(str::to_owned),
|
||||
provider_account_name: account_name.map(str::to_owned),
|
||||
provider_account_search: account_search.map(str::to_owned),
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
@@ -1481,7 +1482,7 @@ async fn queued_history_keeps_frozen_identity_after_configuration_deletion() {
|
||||
let page = repository.list_usage_records(query).await.unwrap();
|
||||
assert_eq!(
|
||||
page.total, expected,
|
||||
"published={published}, key={key_name:?}, account={account_name:?}, status={status:?}"
|
||||
"published={published}, key={key_name:?}, account={account_search:?}, status={status:?}"
|
||||
);
|
||||
assert_eq!(page.items.len() as u64, expected);
|
||||
if expected == 1 {
|
||||
|
||||
@@ -59,10 +59,10 @@ tokio-rustls = { version = "=0.26.4", default-features = false }
|
||||
tokio-util = { version = "0.7.18", features = ["rt"] }
|
||||
tower-service = "0.3.3"
|
||||
tower-layer = "0.3.3"
|
||||
# 官方 Codex WebSocket 需要上游尚未发布的 proxy 与 permessage-deflate 实现,固定到已审计提交。
|
||||
tokio-tungstenite = { git = "https://github.com/openai-oss-forks/tokio-tungstenite", rev = "0e5b2d73aa18dd9f0a50ee9ff199d5aef7594186", features = ["proxy", "rustls-tls-native-roots"] }
|
||||
# 官方 fork 支持代理连接和分帧消息的 permessage-deflate,两个提交须配套
|
||||
tokio-tungstenite = { git = "https://github.com/openai-oss-forks/tokio-tungstenite", rev = "7447c427513cfe685f236e1ac0513c4ea3dd9c0b", features = ["proxy", "rustls-tls-native-roots"] }
|
||||
tracing = "0.1.44"
|
||||
tungstenite = { git = "https://github.com/openai-oss-forks/tungstenite-rs", rev = "4fffad30fe373adbdcffab9545e9e9bf4f2fc19f", features = ["deflate", "proxy"] }
|
||||
tungstenite = { git = "https://github.com/openai-oss-forks/tungstenite-rs", rev = "bc589d684755a8c02da0fc7e58d0861cbfc306f1", features = ["deflate", "proxy"] }
|
||||
url = "2.5.8"
|
||||
uuid = { version = "1.23.3", features = ["v4", "v7"] }
|
||||
# 静态链接 XZ 解码器,不要求部署环境安装 liblzma。
|
||||
@@ -73,8 +73,8 @@ zstd = "=0.14.3"
|
||||
zip = { version = "9.0.4", default-features = false, features = ["deflate-flate2"] }
|
||||
|
||||
[target.'cfg(target_os = "linux")'.dependencies]
|
||||
openssl = "=0.10.80"
|
||||
openssl-sys = { version = "=0.9.116", features = ["vendored"] }
|
||||
openssl = "=0.10.82"
|
||||
openssl-sys = { version = "=0.9.118", features = ["vendored"] }
|
||||
|
||||
[dev-dependencies]
|
||||
gateway-host = { path = "../../gateway-host" }
|
||||
|
||||
@@ -265,8 +265,7 @@ impl ProviderAdmin for OpenAiAdminProvider {
|
||||
}
|
||||
|
||||
fn plan_type_display(&self, plan_type: &str) -> String {
|
||||
// Pro 系列按套餐类型区分展示,其他套餐遵循官方 KnownPlan::display_name
|
||||
// 只映射展示名称,原始套餐值与未知套餐保持不变
|
||||
// Pro 三档沿用管理端短名,其余已知套餐使用 Codex /status 的名称
|
||||
match plan_type.to_ascii_lowercase().as_str() {
|
||||
"free" => "Free",
|
||||
"go" => "Go",
|
||||
@@ -274,16 +273,17 @@ impl ProviderAdmin for OpenAiAdminProvider {
|
||||
"pro" => "Pro",
|
||||
"prolite" => "ProLite",
|
||||
"promax" => "ProMax",
|
||||
"team" => "Team",
|
||||
"self_serve_business_prolite" => "Self Serve Business ProLite",
|
||||
"self_serve_business_usage_based" => "Self Serve Business Usage Based",
|
||||
"business" => "Business",
|
||||
"ent26" | "enterprise" | "hc" => "Enterprise",
|
||||
"team" | "self_serve_business_usage_based" => "Business",
|
||||
"self_serve_business_prolite" => "Business Premium",
|
||||
"business" | "ent26" | "enterprise" | "hc" | "enterprise_cbp_usage_based" => {
|
||||
"Enterprise"
|
||||
}
|
||||
"enterprise_cbp_automation" => "Enterprise (Automation)",
|
||||
"enterprise_cbp_usage_based" => "Enterprise CBP Usage Based",
|
||||
"edu" | "education" => "Edu",
|
||||
"edu_plus" => "Edu Plus",
|
||||
"edu_pro" => "Edu Pro",
|
||||
"sci" => "Academic Researchers",
|
||||
"sci_plus" => "Academic Researchers Plus",
|
||||
_ => plan_type,
|
||||
}
|
||||
.to_owned()
|
||||
|
||||
@@ -33,7 +33,7 @@ use crate::transport::{CodexBackendClient, CodexCatalogModel, CodexRequestContex
|
||||
|
||||
const MAX_RESPONSE_ETAG_BYTES: usize = 256;
|
||||
const ACCOUNT_CATALOG_CACHE_TTL: Duration = Duration::from_secs(5 * 60);
|
||||
const CATALOG_REFRESH_AFTER: Duration = Duration::from_secs(30 * 60);
|
||||
const CATALOG_REFRESH_AFTER: Duration = Duration::from_hours(1);
|
||||
const MAX_ACCOUNT_CATALOG_MODELS: usize = 2_048;
|
||||
const MODEL_CATALOG_TIMEOUT: Duration = Duration::from_secs(15);
|
||||
|
||||
@@ -137,14 +137,31 @@ impl CatalogContent {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AccountEntry {
|
||||
observed_at: SystemTime,
|
||||
content: Arc<CatalogContent>,
|
||||
etag: Option<String>,
|
||||
// 目录返回不同版本时,触发这次查询的标记只用于去重
|
||||
trigger_etag: Option<String>,
|
||||
// 响应只确认这份目录的刷新期限,正文观测时间仍用于并发发布和持久化
|
||||
etag_confirmed_at: std::sync::Mutex<Option<tokio::time::Instant>>,
|
||||
}
|
||||
|
||||
type Entries = BTreeMap<CodexCatalogScope, AccountEntry>;
|
||||
impl AccountEntry {
|
||||
fn recognizes_etag(&self, etag: &str) -> bool {
|
||||
self.etag.as_deref() == Some(etag) || self.trigger_etag.as_deref() == Some(etag)
|
||||
}
|
||||
|
||||
fn is_fresh(&self, now: SystemTime) -> bool {
|
||||
now.duration_since(self.observed_at)
|
||||
.is_ok_and(|age| age < CATALOG_REFRESH_AFTER)
|
||||
|| self.etag_confirmed_at.lock().is_ok_and(|confirmed| {
|
||||
confirmed.is_some_and(|at| at.elapsed() < CATALOG_REFRESH_AFTER)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type Entries = BTreeMap<CodexCatalogScope, Arc<AccountEntry>>;
|
||||
|
||||
pub struct CodexCredentialCatalogSnapshot {
|
||||
models: Vec<CodexCatalogModel>,
|
||||
@@ -551,7 +568,7 @@ impl CodexCredentialCatalogService {
|
||||
revision: account.revision(),
|
||||
plan: account.plan_type().map(str::to_owned),
|
||||
observed_at,
|
||||
etag: etag.clone().or(fetched.etag),
|
||||
etag: fetched.etag.or_else(|| etag.clone()),
|
||||
};
|
||||
if !self
|
||||
.repository
|
||||
@@ -577,7 +594,9 @@ impl CodexCredentialCatalogService {
|
||||
AccountEntry {
|
||||
observed_at,
|
||||
content,
|
||||
trigger_etag: etag.filter(|tag| Some(tag) != observed.etag.as_ref()),
|
||||
etag: observed.etag,
|
||||
etag_confirmed_at: Default::default(),
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
@@ -37,11 +37,13 @@ impl CodexCredentialCatalogService {
|
||||
};
|
||||
entries.insert(
|
||||
scope,
|
||||
AccountEntry {
|
||||
Arc::new(AccountEntry {
|
||||
observed_at: a.observed_at,
|
||||
content: content.clone(),
|
||||
etag: a.etag,
|
||||
},
|
||||
trigger_etag: None,
|
||||
etag_confirmed_at: Default::default(),
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -119,15 +121,21 @@ impl CodexCredentialCatalogService {
|
||||
.cache
|
||||
.write()
|
||||
.map_err(|_| CodexCredentialCatalogError::Cache)?;
|
||||
if state.pending.get(&scope).map(String::as_str) == Some(etag)
|
||||
|| !state.pending.contains_key(&scope)
|
||||
&& state
|
||||
.snapshot
|
||||
.as_ref()
|
||||
.and_then(|s| s.entries.get(&scope))
|
||||
.and_then(|e| e.etag.as_deref())
|
||||
== Some(etag)
|
||||
let pending = state.pending.get(&scope);
|
||||
if pending.is_some_and(|tag| tag == etag) {
|
||||
return Ok(false);
|
||||
}
|
||||
if let Some(entry) = state.snapshot.as_ref().and_then(|s| s.entries.get(&scope))
|
||||
&& entry.recognizes_etag(etag)
|
||||
{
|
||||
// 已收到变更时,迟到的旧标记不能覆盖待刷新版本
|
||||
if pending.is_none() && entry.etag.as_deref() == Some(etag) {
|
||||
let mut confirmed_at = entry
|
||||
.etag_confirmed_at
|
||||
.lock()
|
||||
.map_err(|_| CodexCredentialCatalogError::Cache)?;
|
||||
*confirmed_at = Some(tokio::time::Instant::now());
|
||||
}
|
||||
return Ok(false);
|
||||
}
|
||||
state.pending.insert(scope, etag.to_owned());
|
||||
@@ -166,7 +174,7 @@ impl CodexCredentialCatalogService {
|
||||
.get(&scope)
|
||||
.is_none_or(|old| old.observed_at < entry.observed_at)
|
||||
{
|
||||
entries.insert(scope, entry);
|
||||
entries.insert(scope, Arc::new(entry));
|
||||
}
|
||||
}
|
||||
self.publish(revision, entries)?;
|
||||
@@ -205,7 +213,7 @@ impl CodexCredentialCatalogService {
|
||||
return true;
|
||||
};
|
||||
// 发布其他账号时保留通知,只有该账号更新的观测才能确认 ETag
|
||||
entry.etag.as_deref() != Some(tag)
|
||||
!entry.recognizes_etag(tag)
|
||||
|| old
|
||||
.as_ref()
|
||||
.and_then(|s| s.entries.get(scope))
|
||||
@@ -278,10 +286,7 @@ impl CodexCredentialCatalogService {
|
||||
.into_iter()
|
||||
.filter(|(scope, _)| {
|
||||
pending.contains_key(scope)
|
||||
|| !entries.get(scope).is_some_and(|e| {
|
||||
now.duration_since(e.observed_at)
|
||||
.is_ok_and(|age| age < CATALOG_REFRESH_AFTER)
|
||||
})
|
||||
|| !entries.get(scope).is_some_and(|entry| entry.is_fresh(now))
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
if due.is_empty() {
|
||||
|
||||
@@ -434,10 +434,6 @@ pub(super) struct OpenAiSessionState {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub(super) oauth_identity: Option<String>,
|
||||
pub(super) conversation_id: Option<String>,
|
||||
#[serde(default)]
|
||||
pub(super) turn_state: Option<String>,
|
||||
#[serde(default)]
|
||||
pub(super) client_turn_id: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub(super) reasoning_effort_override: Option<String>,
|
||||
pub(super) continuation_scope: OpenAiContinuationScope,
|
||||
@@ -456,20 +452,12 @@ pub(super) struct OpenAiSessionCapture {
|
||||
pub(super) credential_revision: Option<u64>,
|
||||
pub(super) oauth_identity: Option<String>,
|
||||
pub(super) conversation_id: Option<String>,
|
||||
pub(super) turn_state: Option<String>,
|
||||
pub(super) client_turn_id: Option<String>,
|
||||
pub(super) reasoning_effort_override: Option<String>,
|
||||
pub(super) response_store: bool,
|
||||
pub(super) oversized_http: bool,
|
||||
pub(super) continuation_scope: Option<OpenAiContinuationScope>,
|
||||
}
|
||||
|
||||
pub(super) fn same_client_turn(previous: Option<&str>, current: Option<&str>) -> bool {
|
||||
previous
|
||||
.zip(current)
|
||||
.is_some_and(|(previous, current)| !previous.is_empty() && previous == current)
|
||||
}
|
||||
|
||||
pub(super) fn decode_openai_session_state(request: &GenerateRequest) -> Option<OpenAiSessionState> {
|
||||
request
|
||||
.provider_session_state(PROVIDER_NAME)
|
||||
@@ -505,8 +493,6 @@ fn encode_openai_session_capture(
|
||||
credential_revision: capture.credential_revision,
|
||||
oauth_identity: capture.oauth_identity.clone(),
|
||||
conversation_id: capture.conversation_id.clone(),
|
||||
turn_state: capture.turn_state.clone(),
|
||||
client_turn_id: capture.client_turn_id.clone(),
|
||||
reasoning_effort_override: capture.reasoning_effort_override.clone(),
|
||||
continuation_scope,
|
||||
})
|
||||
@@ -938,7 +924,6 @@ pub(super) fn cold_response_stream(response: ColdResponse) -> EventStream {
|
||||
} else {
|
||||
OpenAiContinuationScope::ReplayRequired
|
||||
});
|
||||
capture.turn_state = response.turn_state.clone().or(capture.turn_state.clone());
|
||||
}
|
||||
let mut observation_state = OpenAiResponseObservationState::from_backend_response(
|
||||
&response,
|
||||
@@ -1070,7 +1055,6 @@ pub(super) fn cold_response_stream(response: ColdResponse) -> EventStream {
|
||||
};
|
||||
let metadata_merge = merge_response_metadata_updates(
|
||||
response_metadata_updates.as_ref(),
|
||||
&mut session_capture,
|
||||
&mut observation_state,
|
||||
)
|
||||
.await;
|
||||
@@ -1148,7 +1132,6 @@ pub(super) fn cold_response_stream(response: ColdResponse) -> EventStream {
|
||||
};
|
||||
let metadata_merge = merge_response_metadata_updates(
|
||||
response_metadata_updates.as_ref(),
|
||||
&mut session_capture,
|
||||
&mut observation_state,
|
||||
)
|
||||
.await;
|
||||
@@ -1354,7 +1337,6 @@ pub(super) fn cold_response_stream(response: ColdResponse) -> EventStream {
|
||||
}
|
||||
let metadata_changed = merge_response_metadata_updates(
|
||||
response_metadata_updates.as_ref(),
|
||||
&mut session_capture,
|
||||
&mut observation_state,
|
||||
)
|
||||
.await
|
||||
@@ -1417,12 +1399,8 @@ pub(super) fn cold_response_stream(response: ColdResponse) -> EventStream {
|
||||
|
||||
async fn merge_response_metadata_updates(
|
||||
updates: Option<&CodexResponseMetadataUpdates>,
|
||||
session_capture: &mut Option<OpenAiSessionCapture>,
|
||||
observation_state: &mut OpenAiResponseObservationState,
|
||||
) -> Option<bool> {
|
||||
let turn_state = updates?.lock().await.turn_state.take()?;
|
||||
if let Some(capture) = session_capture.as_mut() {
|
||||
capture.turn_state = Some(turn_state.clone());
|
||||
}
|
||||
Some(observation_state.merge_client_header("x-codex-turn-state", &turn_state))
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ use crate::transport::protocol::responses::{
|
||||
};
|
||||
use crate::transport::request::{
|
||||
CodexRequestEncodeError, RequestAccountScope, align_structured_location_fields,
|
||||
clear_request_turn_state, encode_generate_request, scope_request_to_account,
|
||||
encode_generate_request, scope_request_to_account,
|
||||
};
|
||||
use crate::transport::session::CodexSessionIdentity;
|
||||
use crate::transport::usage::normalize_service_tier;
|
||||
@@ -240,19 +240,6 @@ impl CodexProvider {
|
||||
if let Some(identity) = &self.session_identity {
|
||||
identity.prepare_local_conversation(&mut upstream);
|
||||
}
|
||||
if let Some(previous_session) = previous_session.as_ref() {
|
||||
if same_client_turn(
|
||||
previous_session.client_turn_id.as_deref(),
|
||||
upstream.client_turn_id.as_deref(),
|
||||
) {
|
||||
upstream.turn_state = upstream
|
||||
.turn_state
|
||||
.take()
|
||||
.or_else(|| previous_session.turn_state.clone());
|
||||
} else {
|
||||
clear_request_turn_state(&mut upstream);
|
||||
}
|
||||
}
|
||||
let session_affinity =
|
||||
derive_codex_session_affinity(&upstream, context.client_api_key_ref());
|
||||
let cyber_policy_session_key =
|
||||
@@ -1144,8 +1131,6 @@ impl CodexProvider {
|
||||
)
|
||||
.then_some(lease.account().revision().get()),
|
||||
conversation_id: upstream_request.local_conversation_id.clone(),
|
||||
turn_state: upstream_request.turn_state.clone(),
|
||||
client_turn_id: upstream_request.client_turn_id.clone(),
|
||||
reasoning_effort_override: upstream_request
|
||||
.semantics(
|
||||
previous_session
|
||||
|
||||
@@ -701,8 +701,6 @@ pub(super) fn codex_request_context<'a>(
|
||||
request_id,
|
||||
turn_state: request.turn_state.as_deref(),
|
||||
turn_metadata: request.turn_metadata.as_deref(),
|
||||
beta_features: request.beta_features.as_deref(),
|
||||
include_timing_metrics: request.include_timing_metrics.as_deref(),
|
||||
version: request.version.as_deref(),
|
||||
codex_window_id: request.codex_window_id.as_deref(),
|
||||
parent_thread_id: request.parent_thread_id.as_deref(),
|
||||
@@ -711,7 +709,6 @@ pub(super) fn codex_request_context<'a>(
|
||||
session_id: request.client_session_id.as_deref(),
|
||||
thread_id: request.client_thread_id.as_deref(),
|
||||
client_request_id: request.client_request_id.as_deref(),
|
||||
turn_id: request.client_turn_id.as_deref(),
|
||||
account_selection,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ pub(crate) fn follows_session_with_headers(
|
||||
}
|
||||
gateway_protocol::openai::codex_responses_request_semantics_with_turn_metadata(
|
||||
body,
|
||||
metadata.or_else(|| context.get("turn_metadata").and_then(Value::as_str)),
|
||||
metadata.or_else(|| gateway_protocol::openai::context_turn_metadata(body, context)),
|
||||
None,
|
||||
)
|
||||
.subagent_kind
|
||||
|
||||
@@ -472,10 +472,6 @@ pub struct CodexRequestContext<'a> {
|
||||
pub turn_state: Option<&'a str>,
|
||||
/// 客户端 turn metadata;其中 installation ID 已按当前账号处理
|
||||
pub turn_metadata: Option<&'a str>,
|
||||
/// x-codex-beta-features 头值
|
||||
pub beta_features: Option<&'a str>,
|
||||
/// x-responsesapi-include-timing-metrics 头值
|
||||
pub include_timing_metrics: Option<&'a str>,
|
||||
/// 下游 `version` 扩展头;存在时上游值由 Desktop 版本画像统一生成
|
||||
pub version: Option<&'a str>,
|
||||
/// 客户端 window ID
|
||||
@@ -492,8 +488,6 @@ pub struct CodexRequestContext<'a> {
|
||||
pub thread_id: Option<&'a str>,
|
||||
/// 客户端逻辑 request ID;缺失时使用代理请求 ID
|
||||
pub client_request_id: Option<&'a str>,
|
||||
/// 客户端 turn ID
|
||||
pub turn_id: Option<&'a str>,
|
||||
/// 账号亲和选择结果;仅用于结构化遥测,不参与 transport 决策
|
||||
pub account_selection: CodexAccountSelectionTelemetry<'a>,
|
||||
}
|
||||
@@ -514,8 +508,6 @@ impl<'a> CodexRequestContext<'a> {
|
||||
request_id,
|
||||
turn_state: None,
|
||||
turn_metadata: None,
|
||||
beta_features: None,
|
||||
include_timing_metrics: None,
|
||||
version: None,
|
||||
codex_window_id: None,
|
||||
parent_thread_id: None,
|
||||
@@ -524,7 +516,6 @@ impl<'a> CodexRequestContext<'a> {
|
||||
session_id: None,
|
||||
thread_id: None,
|
||||
client_request_id: None,
|
||||
turn_id: None,
|
||||
account_selection: CodexAccountSelectionTelemetry::NONE,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -211,11 +211,6 @@ impl CodexBackendClient {
|
||||
("x-codex-window-id", context.codex_window_id),
|
||||
("x-codex-turn-state", context.turn_state),
|
||||
("x-codex-turn-metadata", context.turn_metadata),
|
||||
("x-codex-beta-features", context.beta_features),
|
||||
(
|
||||
"x-responsesapi-include-timing-metrics",
|
||||
context.include_timing_metrics,
|
||||
),
|
||||
("x-codex-parent-thread-id", context.parent_thread_id),
|
||||
(
|
||||
X_OPENAI_MEMGEN_REQUEST_HEADER,
|
||||
|
||||
@@ -56,10 +56,10 @@ pub(super) struct DesktopArtifact {
|
||||
pub etag: &'static str,
|
||||
}
|
||||
|
||||
// 官方 npm 稳定标签及六个平台依赖;核验于 2026-11-14T02:09:52Z
|
||||
// 官方 npm 稳定版本及六个平台依赖,核验于 2026-11-19T00:51:11Z
|
||||
pub(super) const CLI_RELEASE: CoreRelease = CoreRelease {
|
||||
version: "0.169.0",
|
||||
verified_at_millis: 1_794_622_192_000,
|
||||
version: "0.170.0",
|
||||
verified_at_millis: 1_795_049_471_000,
|
||||
};
|
||||
|
||||
// macOS arm64 ZIP;核验于 2026-11-14T02:09:52Z
|
||||
|
||||
@@ -50,8 +50,6 @@ pub struct CodexResponsesRequest {
|
||||
pub client_thread_id: Option<String>,
|
||||
/// 客户端 request ID,仅保留在受控本地上下文
|
||||
pub client_request_id: Option<String>,
|
||||
/// 客户端 turn ID,仅保留在受控本地上下文
|
||||
pub client_turn_id: Option<String>,
|
||||
/// 连接池和 affinity 使用的本地会话身份,不发送上游
|
||||
pub local_conversation_id: Option<String>,
|
||||
/// 变体身份键
|
||||
@@ -70,12 +68,10 @@ pub struct CodexResponsesRequest {
|
||||
pub turn_state: Option<String>,
|
||||
/// turn metadata 透传头
|
||||
pub turn_metadata: Option<String>,
|
||||
/// beta features 透传头
|
||||
pub beta_features: Option<String>,
|
||||
/// 实际入站为 WS 帧时,观测优先使用本帧的轮次元数据
|
||||
pub(crate) websocket_frame: bool,
|
||||
/// 下游 `version` 扩展头;存在时上游值由 Desktop 版本画像统一生成
|
||||
pub version: Option<String>,
|
||||
/// timing metrics 透传头
|
||||
pub include_timing_metrics: Option<String>,
|
||||
/// Responses Lite 请求语义;HTTP 使用 header,WebSocket 使用 client metadata 投影
|
||||
pub responses_lite: Option<String>,
|
||||
/// Memory consolidation 请求语义;HTTP 与 WebSocket opening 均使用 header
|
||||
@@ -605,7 +601,6 @@ impl CodexResponsesRequest {
|
||||
client_account_follow_only: false,
|
||||
client_thread_id: None,
|
||||
client_request_id: None,
|
||||
client_turn_id: None,
|
||||
local_conversation_id: None,
|
||||
variant_identity: None,
|
||||
client_ip: None,
|
||||
@@ -615,9 +610,8 @@ impl CodexResponsesRequest {
|
||||
force_http_sse: false,
|
||||
turn_state: None,
|
||||
turn_metadata: None,
|
||||
beta_features: None,
|
||||
websocket_frame: false,
|
||||
version: None,
|
||||
include_timing_metrics: None,
|
||||
responses_lite: None,
|
||||
memgen_request: None,
|
||||
codex_window_id: None,
|
||||
@@ -774,7 +768,11 @@ impl CodexResponsesRequest {
|
||||
) -> CodexRequestSemantics {
|
||||
codex_responses_request_semantics_with_turn_metadata(
|
||||
self.body(),
|
||||
self.turn_metadata.as_deref(),
|
||||
gateway_protocol::openai::observed_turn_metadata(
|
||||
self.body(),
|
||||
self.turn_metadata.as_deref(),
|
||||
self.websocket_frame,
|
||||
),
|
||||
previous_reasoning_effort_override,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -198,7 +198,7 @@ pub fn websocket_response_create_payload(request: &CodexResponsesRequest) -> Val
|
||||
Value::Object(payload)
|
||||
}
|
||||
|
||||
/// 借用原始 body 序列化 `response.create` 帧,不复制字段
|
||||
/// 借用原始 body 序列化 `response.create` 消息,不复制字段
|
||||
///
|
||||
/// 输出与序列化 [`websocket_response_create_payload`] 的合并 Map 逐字节一致:
|
||||
/// body 自带 `type` 键时以 body 值置于首位,其余字段按插入顺序原样透传
|
||||
@@ -224,7 +224,7 @@ impl serde::Serialize for ResponseCreateFrame<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// 生成 Responses WebSocket `response.create` 文本帧内容
|
||||
/// 生成 Responses WebSocket `response.create` 消息的完整文本
|
||||
pub fn websocket_response_create_payload_text(
|
||||
request: &CodexResponsesRequest,
|
||||
) -> Result<String, serde_json::Error> {
|
||||
|
||||
@@ -22,7 +22,6 @@ use crate::transport::protocol::responses::{
|
||||
};
|
||||
|
||||
pub(crate) const PASSTHROUGH_HEADERS_CONTEXT_KEY: &str = "opaque_request_headers";
|
||||
const TURN_ID_CLIENT_METADATA_KEY: &str = "turn_id";
|
||||
const THREAD_SPAWN_SUBAGENT_KIND: &str = "thread_spawn";
|
||||
const THREAD_SPAWN_CONVERSATION_PREFIX: &str = "thread-spawn:";
|
||||
const ENVIRONMENT_CONTEXT_CONTENT_KIND: &str = "environments.environment_context";
|
||||
@@ -256,14 +255,11 @@ fn extract_request_context(request: &mut CodexResponsesRequest) {
|
||||
let context = ExtractedRequestContext::from_body(request.body());
|
||||
request.turn_state = context.turn_state;
|
||||
request.turn_metadata = context.turn_metadata;
|
||||
request.beta_features = context.beta_features;
|
||||
request.version = context.version;
|
||||
request.include_timing_metrics = context.include_timing_metrics;
|
||||
request.codex_window_id = context.codex_window_id;
|
||||
request.parent_thread_id = context.parent_thread_id;
|
||||
request.client_conversation_id = context.conversation_id;
|
||||
request.client_request_id = context.client_request_id;
|
||||
request.client_turn_id = context.turn_id;
|
||||
request.responses_lite = context.responses_lite;
|
||||
request.memgen_request = context.memgen_request;
|
||||
}
|
||||
@@ -271,14 +267,11 @@ fn extract_request_context(request: &mut CodexResponsesRequest) {
|
||||
struct ExtractedRequestContext {
|
||||
turn_state: Option<String>,
|
||||
turn_metadata: Option<String>,
|
||||
beta_features: Option<String>,
|
||||
version: Option<String>,
|
||||
include_timing_metrics: Option<String>,
|
||||
codex_window_id: Option<String>,
|
||||
parent_thread_id: Option<String>,
|
||||
conversation_id: Option<String>,
|
||||
client_request_id: Option<String>,
|
||||
turn_id: Option<String>,
|
||||
responses_lite: Option<String>,
|
||||
memgen_request: Option<String>,
|
||||
}
|
||||
@@ -287,26 +280,18 @@ impl ExtractedRequestContext {
|
||||
fn from_body(body: &Map<String, Value>) -> Self {
|
||||
let client_metadata = body.get("client_metadata").and_then(Value::as_object);
|
||||
Self {
|
||||
// 官方 downstream WebSocket 无法逐帧更新 HTTP header,因此把
|
||||
// response.metadata 返回的 turn state 放回下一帧 client_metadata
|
||||
// Provider 仍会在账号/turn 归属确定后决定是否允许复用该状态
|
||||
// 下游 WS 在每个 response.create 的 client_metadata 回传本轮路由状态
|
||||
turn_state: body_string(body, "turnState").or_else(|| {
|
||||
client_metadata.and_then(|metadata| {
|
||||
string_value(metadata.get(X_CODEX_TURN_STATE_CLIENT_METADATA_KEY))
|
||||
})
|
||||
}),
|
||||
turn_metadata: body_string(body, "turnMetadata"),
|
||||
beta_features: body_string(body, "betaFeatures"),
|
||||
version: body_string(body, "version"),
|
||||
include_timing_metrics: body_string(body, "includeTimingMetrics"),
|
||||
codex_window_id: body_string(body, "codexWindowId"),
|
||||
parent_thread_id: body_string(body, "parentThreadId"),
|
||||
conversation_id: body_string(body, "conversation_id"),
|
||||
client_request_id: body_string(body, "x-client-request-id"),
|
||||
turn_id: body_string(body, "turn_id").or_else(|| {
|
||||
client_metadata
|
||||
.and_then(|metadata| string_value(metadata.get(TURN_ID_CLIENT_METADATA_KEY)))
|
||||
}),
|
||||
// Responses Lite 在 WebSocket body 中的这个键是官方 header 投影,
|
||||
// 不是普通上下文字段的 metadata 回退
|
||||
responses_lite: client_metadata.and_then(|metadata| {
|
||||
@@ -452,8 +437,8 @@ fn normalize_conversation_anchor_text(text: &str) -> String {
|
||||
rest.to_owned()
|
||||
}
|
||||
|
||||
/// 已确认换轮或换号时,各 wire 入口同步清理原有路由 token
|
||||
pub(crate) fn clear_request_turn_state(request: &mut CodexResponsesRequest) {
|
||||
/// 明确换号时,同步清除请求头与正文中的原账号路由 token
|
||||
fn clear_request_turn_state(request: &mut CodexResponsesRequest) {
|
||||
request.turn_state = None;
|
||||
request.passthrough_headers.remove("x-codex-turn-state");
|
||||
for key in TURN_STATE_KEYS {
|
||||
@@ -745,20 +730,24 @@ fn replace_metadata_field(metadata: &mut Map<String, Value>, key: &str, value: O
|
||||
// 请求头的场景)
|
||||
fn apply_protocol_context(request: &mut CodexResponsesRequest, context: &Map<String, Value>) {
|
||||
request.passthrough_headers = decode_passthrough_headers(context);
|
||||
request.turn_state =
|
||||
context_string(context, "turn_state").or_else(|| request.turn_state.take());
|
||||
request.websocket_frame = context
|
||||
.get(gateway_protocol::openai::WEBSOCKET_FRAME_CONTEXT_KEY)
|
||||
.and_then(Value::as_bool)
|
||||
== Some(true);
|
||||
request.turn_metadata =
|
||||
context_string(context, "turn_metadata").or_else(|| request.turn_metadata.take());
|
||||
request.beta_features =
|
||||
context_string(context, "beta_features").or_else(|| request.beta_features.take());
|
||||
request.version = context_string(context, "version").or_else(|| request.version.take());
|
||||
request.include_timing_metrics = context_string(context, "include_timing_metrics")
|
||||
.or_else(|| request.include_timing_metrics.take());
|
||||
request.codex_window_id =
|
||||
context_string(context, "codex_window_id").or_else(|| request.codex_window_id.take());
|
||||
request.downstream_websocket_connection_id =
|
||||
context_string(context, "downstream_websocket_connection_id")
|
||||
.or_else(|| request.downstream_websocket_connection_id.take());
|
||||
if request.downstream_websocket_connection_id.is_some() && request.turn_state.is_some() {
|
||||
// 当前帧的路由状态优先于握手值,原始握手头也不能覆盖出站投影
|
||||
request.passthrough_headers.remove("x-codex-turn-state");
|
||||
} else if let Some(state) = context_string(context, "turn_state") {
|
||||
request.turn_state = Some(state);
|
||||
}
|
||||
request.parent_thread_id =
|
||||
context_string(context, "parent_thread_id").or_else(|| request.parent_thread_id.take());
|
||||
let prompt_cache_key = request.prompt_cache_key().map(ToOwned::to_owned);
|
||||
@@ -773,8 +762,6 @@ fn apply_protocol_context(request: &mut CodexResponsesRequest, context: &Map<Str
|
||||
request.client_thread_id = gateway_protocol::openai::codex_thread_id(request.body(), context);
|
||||
request.client_request_id =
|
||||
context_string(context, "client_request_id").or_else(|| request.client_request_id.take());
|
||||
request.client_turn_id =
|
||||
context_string(context, "turn_id").or_else(|| request.client_turn_id.take());
|
||||
request.responses_lite =
|
||||
context_string(context, "responses_lite").or_else(|| request.responses_lite.take());
|
||||
request.memgen_request =
|
||||
|
||||
@@ -251,6 +251,27 @@ struct PricingRule {
|
||||
// 已按 https://developers.openai.com/api/docs/deprecations 核验至 2026-09-13,
|
||||
// 移除已关闭的型号;仅宣布弃用但尚未到关闭日期的型号继续保留
|
||||
const PRICING_RULES: &[PricingRule] = &[
|
||||
// GPT-6.2 的 Ultrafast 使用官方价目表中的 6 倍常规价,核验日期 2026-11-18
|
||||
PricingRule {
|
||||
model: "gpt-6.2-astra",
|
||||
pricing: ModelPricing::new(80_000, 400_000, 4_000)
|
||||
.with_cache_write(125)
|
||||
.with_fast(160_000, 800_000, 8_000)
|
||||
.with_ultrafast(480_000, 2_400_000, 24_000)
|
||||
.with_long(160_000, 600_000, 8_000)
|
||||
.with_long_fast(320_000, 1_200_000, 16_000)
|
||||
.with_long_ultrafast(960_000, 3_600_000, 48_000),
|
||||
},
|
||||
PricingRule {
|
||||
model: "gpt-6.2-sol",
|
||||
pricing: ModelPricing::new(20_000, 100_000, 1_000)
|
||||
.with_cache_write(125)
|
||||
.with_fast(40_000, 200_000, 2_000)
|
||||
.with_ultrafast(120_000, 600_000, 6_000)
|
||||
.with_long(40_000, 150_000, 2_000)
|
||||
.with_long_fast(80_000, 300_000, 4_000)
|
||||
.with_long_ultrafast(240_000, 900_000, 12_000),
|
||||
},
|
||||
// Astra:https://developers.openai.com/api/docs/models/gpt-6-astra
|
||||
// 已于 2026-09-09 对照官方价目表核验
|
||||
PricingRule {
|
||||
@@ -924,7 +945,13 @@ fn reasoning_model(model: &str) -> bool {
|
||||
model.starts_with("gpt-5")
|
||||
|| matches!(
|
||||
model,
|
||||
"gpt-6-astra" | "gpt-6.1-sol" | "gpt-6.1-terra" | "gpt-6-sol" | "gpt-6-luna"
|
||||
"gpt-6.2-astra"
|
||||
| "gpt-6.2-sol"
|
||||
| "gpt-6-astra"
|
||||
| "gpt-6.1-sol"
|
||||
| "gpt-6.1-terra"
|
||||
| "gpt-6-sol"
|
||||
| "gpt-6-luna"
|
||||
)
|
||||
|| model.starts_with("o1")
|
||||
|| model.starts_with("o3")
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! Responses WebSocket endpoint、opening handshake 与首帧发送
|
||||
//! Responses WebSocket endpoint、opening handshake 与请求发送
|
||||
|
||||
use std::{error::Error as _, io, time::Duration};
|
||||
|
||||
@@ -95,7 +95,7 @@ impl CodexWebSocketConnection {
|
||||
}
|
||||
}
|
||||
|
||||
/// 构造 Responses WebSocket opening 与首个 `response.create` 文本帧
|
||||
/// 构造 Responses WebSocket opening 与首条 `response.create` 消息
|
||||
pub fn responses_create_request(
|
||||
base_url: &str,
|
||||
websocket_key: &str,
|
||||
|
||||
@@ -147,7 +147,7 @@ impl CodexWebSocketRequest {
|
||||
&self.connection
|
||||
}
|
||||
|
||||
/// 返回将要发送的首个文本帧
|
||||
/// 返回将要发送的完整 `response.create` 文本
|
||||
pub fn payload_text(&self) -> &str {
|
||||
&self.payload_text
|
||||
}
|
||||
|
||||
@@ -11,9 +11,13 @@ use std::sync::{
|
||||
};
|
||||
use std::time::Duration;
|
||||
|
||||
use bytes::Bytes;
|
||||
use futures::{Sink, SinkExt, Stream, StreamExt};
|
||||
use tokio::{sync::mpsc, task::JoinHandle, time::Instant};
|
||||
use tokio_tungstenite::tungstenite::Message;
|
||||
use tungstenite::{
|
||||
Message,
|
||||
protocol::frame::{Fragment, Frame, coding::Data},
|
||||
};
|
||||
use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
@@ -311,7 +315,7 @@ impl PumpedWebSocket {
|
||||
}
|
||||
}
|
||||
|
||||
/// 通过 pump 发送一帧,区分未开始发送与底层传输失败
|
||||
/// 通过 pump 发送一条消息,区分未开始发送与底层传输失败
|
||||
pub(crate) async fn send(&self, message: Message) -> Result<(), CodexWebSocketExchangeError> {
|
||||
if self.is_closed() {
|
||||
return Err(CodexWebSocketExchangeError::SendNotStarted);
|
||||
@@ -613,7 +617,7 @@ async fn handle_command(
|
||||
return None;
|
||||
}
|
||||
let is_close = matches!(message, Message::Close(_));
|
||||
let result = inner.send(message).await;
|
||||
let result = write_message(inner, message).await;
|
||||
let transport_error = result
|
||||
.as_ref()
|
||||
.err()
|
||||
@@ -629,6 +633,31 @@ async fn handle_command(
|
||||
}
|
||||
}
|
||||
|
||||
/// 同一命令内逐帧写完大文本,保持一条消息的压缩上下文和发送回执
|
||||
async fn write_message(
|
||||
inner: &mut RawWsStream,
|
||||
message: Message,
|
||||
) -> Result<(), tungstenite::Error> {
|
||||
const FRAGMENT_BYTES: usize = 1024 * 1024;
|
||||
let mut remaining = match message {
|
||||
Message::Text(text) if text.len() > FRAGMENT_BYTES => Bytes::from(text),
|
||||
other => return inner.send(other).await,
|
||||
};
|
||||
let mut opcode = Data::Text;
|
||||
while !remaining.is_empty() {
|
||||
let payload = remaining.split_to(remaining.len().min(FRAGMENT_BYTES));
|
||||
let fragment = if remaining.is_empty() {
|
||||
Fragment::Final
|
||||
} else {
|
||||
Fragment::More
|
||||
};
|
||||
let frame = Frame::message_fragment(payload, opcode, fragment);
|
||||
inner.send(Message::Frame(frame)).await?;
|
||||
opcode = Data::Continue;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn record_activity(lifecycle: &Mutex<PumpLifecycleState>, observed_at: Instant) {
|
||||
lifecycle
|
||||
.lock()
|
||||
|
||||
@@ -69,7 +69,7 @@ const COMPLETED_SESSION_SSE: &str = concat!(
|
||||
);
|
||||
|
||||
#[tokio::test]
|
||||
async fn plan_display_should_distinguish_pro_tiers_and_preserve_other_official_names() {
|
||||
async fn plan_display_uses_pro_short_names_and_codex_status_labels_for_other_known_plans() {
|
||||
let config = valid_config();
|
||||
let bundle = provider_openai::initialize(
|
||||
config.config.clone(),
|
||||
@@ -86,25 +86,25 @@ async fn plan_display_should_distinguish_pro_tiers_and_preserve_other_official_n
|
||||
("free", "Free"),
|
||||
("go", "Go"),
|
||||
("plus", "Plus"),
|
||||
("team", "Team"),
|
||||
("self_serve_business_prolite", "Self Serve Business ProLite"),
|
||||
(
|
||||
"self_serve_business_usage_based",
|
||||
"Self Serve Business Usage Based",
|
||||
),
|
||||
("business", "Business"),
|
||||
("team", "Business"),
|
||||
("self_serve_business_prolite", "Business Premium"),
|
||||
("self_serve_business_usage_based", "Business"),
|
||||
("business", "Enterprise"),
|
||||
("ent26", "Enterprise"),
|
||||
("enterprise", "Enterprise"),
|
||||
("hc", "Enterprise"),
|
||||
("enterprise_cbp_automation", "Enterprise (Automation)"),
|
||||
("enterprise_cbp_usage_based", "Enterprise CBP Usage Based"),
|
||||
("enterprise_cbp_usage_based", "Enterprise"),
|
||||
("edu", "Edu"),
|
||||
("education", "Edu"),
|
||||
("edu_plus", "Edu Plus"),
|
||||
("edu_pro", "Edu Pro"),
|
||||
("future_plan", "future_plan"),
|
||||
("sci", "Academic Researchers"),
|
||||
("sci_plus", "Academic Researchers Plus"),
|
||||
("SCI_PLUS", "Academic Researchers Plus"),
|
||||
("Future_Plan", "Future_Plan"),
|
||||
] {
|
||||
assert_eq!(admin.plan_type_display(raw), display);
|
||||
assert_eq!(admin.plan_type_display(raw), display, "{raw}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -523,13 +523,18 @@ async fn copying_builtin_prices_keeps_cache_read_and_write_fallback_costs() {
|
||||
for model in [
|
||||
"gpt-4",
|
||||
"gpt-4o",
|
||||
"gpt-6.2-astra",
|
||||
"gpt-6.2-sol",
|
||||
"gpt-6-astra",
|
||||
"gpt-6.1-sol",
|
||||
"gpt-6.1-terra",
|
||||
"gpt-6-sol",
|
||||
"gpt-6-luna",
|
||||
] {
|
||||
let ultrafast_supported = matches!(model, "gpt-6-astra" | "gpt-6.1-sol");
|
||||
let ultrafast_supported = matches!(
|
||||
model,
|
||||
"gpt-6.2-astra" | "gpt-6.2-sol" | "gpt-6-astra" | "gpt-6.1-sol"
|
||||
);
|
||||
for band in ["ultrafast", "long_ultrafast"] {
|
||||
assert_eq!(prices[model].bands.contains_key(band), ultrafast_supported);
|
||||
}
|
||||
|
||||
@@ -738,6 +738,108 @@ async fn invalidation_queues_background_refresh_without_an_upstream_etag() {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn matching_etag_renews_only_its_account_until_the_next_change_or_expiry() {
|
||||
use gateway_core::account::AccountCatalogObservation;
|
||||
use std::time::{Duration, SystemTime};
|
||||
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
let active = seed_account(&store, "acct_active").await;
|
||||
let idle = seed_account(&store, "acct_idle").await;
|
||||
let document = [&[0_u8][..], OFFICIAL_FIXTURE].concat();
|
||||
let old_tag = r#""v1""#;
|
||||
let new_tag = r#""v2""#;
|
||||
let observed_at = SystemTime::now() - Duration::from_mins(61);
|
||||
for account in [&active, &idle] {
|
||||
assert!(
|
||||
store
|
||||
.save_account_catalog(
|
||||
&AccountCatalogObservation {
|
||||
account_id: account.id().clone(),
|
||||
revision: account.revision(),
|
||||
plan: account.plan_type().map(str::to_owned),
|
||||
observed_at,
|
||||
etag: Some(old_tag.to_owned()),
|
||||
},
|
||||
&document
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
);
|
||||
}
|
||||
let server = MockServer::start().await;
|
||||
for account in [&active, &idle] {
|
||||
Mock::given(method("GET"))
|
||||
.and(path("/codex/models"))
|
||||
.and(header(
|
||||
"chatgpt-account-id",
|
||||
format!("chatgpt-{}", account.id()),
|
||||
))
|
||||
.respond_with(
|
||||
ResponseTemplate::new(200)
|
||||
.insert_header("etag", old_tag)
|
||||
.set_body_raw(OFFICIAL_FIXTURE, "application/json"),
|
||||
)
|
||||
.expect(1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
}
|
||||
let service = service_with_catalog_cache(&store, server.uri(), catalog_cache());
|
||||
service.restore().await.unwrap();
|
||||
assert!(!service.observe_response_etag(&active, old_tag).unwrap());
|
||||
service.refresh_catalogs().await.unwrap();
|
||||
let requests = server.received_requests().await.unwrap();
|
||||
assert_eq!(requests.len(), 1);
|
||||
assert_eq!(
|
||||
requests[0].headers["chatgpt-account-id"],
|
||||
"chatgpt-acct_idle"
|
||||
);
|
||||
|
||||
tokio::time::pause();
|
||||
tokio::time::advance(Duration::from_mins(59)).await;
|
||||
service.refresh_catalogs().await.unwrap();
|
||||
assert_eq!(server.received_requests().await.unwrap().len(), 1);
|
||||
tokio::time::advance(Duration::from_mins(1)).await;
|
||||
tokio::time::resume();
|
||||
service.refresh_catalogs().await.unwrap();
|
||||
server.verify().await;
|
||||
|
||||
server.reset().await;
|
||||
Mock::given(method("GET"))
|
||||
.and(path("/codex/models"))
|
||||
.and(header("chatgpt-account-id", "chatgpt-acct_active"))
|
||||
.respond_with(
|
||||
ResponseTemplate::new(200)
|
||||
.insert_header("etag", new_tag)
|
||||
.set_body_json(
|
||||
serde_json::json!({"models":[{"slug":"active-only","display_name":"Active"}]}),
|
||||
),
|
||||
)
|
||||
.expect(1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
assert!(!service.observe_response_etag(&active, old_tag).unwrap());
|
||||
assert!(
|
||||
service
|
||||
.observe_response_etag(&active, "change-hint")
|
||||
.unwrap()
|
||||
);
|
||||
assert!(!service.observe_response_etag(&active, old_tag).unwrap());
|
||||
service.refresh_catalogs().await.unwrap();
|
||||
assert!(
|
||||
!service
|
||||
.observe_response_etag(&active, "change-hint")
|
||||
.unwrap()
|
||||
);
|
||||
assert!(!service.observe_response_etag(&active, new_tag).unwrap());
|
||||
service.refresh_catalogs().await.unwrap();
|
||||
assert_eq!(
|
||||
service.cached_account_models(&active).unwrap().unwrap(),
|
||||
["active-only"]
|
||||
);
|
||||
server.verify().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn simultaneous_periodic_refreshes_share_the_completed_scan() {
|
||||
let store = Arc::new(MemoryAccountStore::default());
|
||||
|
||||
@@ -48,13 +48,15 @@ fn isolation_payload() -> ProtocolPayload {
|
||||
turn_metadata.insert("parent_response_id".to_owned(), json!("client-correlation"));
|
||||
turn_metadata.insert("workspace".to_owned(), json!("/工作区"));
|
||||
turn_metadata.insert("future".to_owned(), json!({"traceparent":"business-value"}));
|
||||
let mut frame_metadata = turn_metadata.clone();
|
||||
frame_metadata.insert("tool_namespaces_info".into(), json!({"workspace":{}}));
|
||||
let mut body = json!({
|
||||
"model": "gpt-5.4",
|
||||
"input": isolation_history(),
|
||||
"client_metadata": {
|
||||
"parent_response_id": "resp_parent_old",
|
||||
"guardian_credits_requested": "true",
|
||||
"x-codex-turn-metadata": Value::Object(turn_metadata.clone()).to_string(),
|
||||
"x-codex-turn-metadata": Value::Object(frame_metadata).to_string(),
|
||||
"turnMetadata": turn_metadata,
|
||||
"mcp_attribution": r#"{"status":"complete","sources":[]}"#
|
||||
}
|
||||
@@ -86,6 +88,10 @@ fn isolation_payload() -> ProtocolPayload {
|
||||
ProtocolPayload::json_object("openai", body.as_object().unwrap().clone())
|
||||
.unwrap()
|
||||
.with_context(Map::from_iter([
|
||||
(
|
||||
gateway_protocol::openai::WEBSOCKET_FRAME_CONTEXT_KEY.into(),
|
||||
json!(true),
|
||||
),
|
||||
("opaque_request_headers".to_owned(), json!(headers)),
|
||||
(
|
||||
"turn_metadata".to_owned(),
|
||||
@@ -155,7 +161,6 @@ fn assert_isolated_request(
|
||||
&body["turnMetadata"],
|
||||
&body["turn_metadata"],
|
||||
&body["client_metadata"]["turnMetadata"],
|
||||
&body["client_metadata"]["x-codex-turn-metadata"],
|
||||
] {
|
||||
let actual = match metadata {
|
||||
Value::String(raw) => serde_json::from_str(raw).unwrap(),
|
||||
@@ -163,6 +168,15 @@ fn assert_isolated_request(
|
||||
};
|
||||
assert_eq!(actual, Value::Object(expected_turn_metadata.clone()));
|
||||
}
|
||||
let mut expected_frame_metadata = expected_turn_metadata.clone();
|
||||
expected_frame_metadata.insert("tool_namespaces_info".into(), json!({"workspace":{}}));
|
||||
let frame = body["client_metadata"]["x-codex-turn-metadata"]
|
||||
.as_str()
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(frame).unwrap(),
|
||||
Value::Object(expected_frame_metadata)
|
||||
);
|
||||
let header = headers["x-codex-turn-metadata"].to_str().unwrap();
|
||||
assert!(header.is_ascii());
|
||||
assert_eq!(
|
||||
|
||||
@@ -2053,8 +2053,6 @@ async fn provider_should_override_unclassified_time_contexts_over_websocket() {
|
||||
}
|
||||
|
||||
async fn capture_turn_state_request(
|
||||
request_id: &str,
|
||||
previous_turn_id: Option<&str>,
|
||||
current_turn_id: Option<&str>,
|
||||
client_turn_state: Option<&str>,
|
||||
) -> wiremock::Request {
|
||||
@@ -2072,18 +2070,19 @@ async fn capture_turn_state_request(
|
||||
.expect(1)
|
||||
.mount(&server)
|
||||
.await;
|
||||
let mut session_state = Map::from_iter([
|
||||
let session_state = Map::from_iter([
|
||||
("account_id".to_owned(), json!(account_id)),
|
||||
("conversation_id".to_owned(), json!("conversation")),
|
||||
("turn_state".to_owned(), json!("previous-turn-state")),
|
||||
("client_turn_id".to_owned(), json!("turn-same")),
|
||||
("continuation_scope".to_owned(), json!("persisted")),
|
||||
]);
|
||||
if let Some(previous_turn_id) = previous_turn_id {
|
||||
session_state.insert("client_turn_id".to_owned(), json!(previous_turn_id));
|
||||
}
|
||||
let mut protocol_context = Map::from_iter([("use_websocket".to_owned(), json!(false))]);
|
||||
if let Some(current_turn_id) = current_turn_id {
|
||||
protocol_context.insert("turn_id".to_owned(), json!(current_turn_id));
|
||||
protocol_context.insert(
|
||||
"turn_metadata".to_owned(),
|
||||
Value::String(json!({"turn_id": current_turn_id}).to_string()),
|
||||
);
|
||||
}
|
||||
if let Some(client_turn_state) = client_turn_state {
|
||||
protocol_context.insert("turn_state".to_owned(), json!(client_turn_state));
|
||||
@@ -2095,6 +2094,7 @@ async fn capture_turn_state_request(
|
||||
let mut body = Map::from_iter([
|
||||
("model".to_owned(), json!("gpt-5.4")),
|
||||
("input".to_owned(), json!("current input")),
|
||||
("previous_response_id".to_owned(), json!("resp_previous")),
|
||||
("client_metadata".to_owned(), json!({"custom":"preserved"})),
|
||||
]);
|
||||
if let Some(state) = client_turn_state {
|
||||
@@ -2119,7 +2119,7 @@ async fn capture_turn_state_request(
|
||||
let mut stream = provider_with_base_url(&store, server.uri())
|
||||
.execute(
|
||||
planned_request("openai", operation),
|
||||
context(request_id, CancellationToken::new()),
|
||||
context("req_client_turn_state", CancellationToken::new()),
|
||||
)
|
||||
.await
|
||||
.expect("prepare turn-state provider stream");
|
||||
@@ -7093,78 +7093,29 @@ async fn missing_affinity_full_request_should_preserve_client_turn_state() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn matching_turn_id_should_restore_previous_turn_state() {
|
||||
let request = capture_turn_state_request(
|
||||
"req_restore_same_turn_state",
|
||||
Some("turn-same"),
|
||||
Some("turn-same"),
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(
|
||||
captured_header_values(&request, "x-codex-turn-state"),
|
||||
vec![b"previous-turn-state".to_vec()]
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn matching_turn_id_should_prefer_an_explicit_client_echo_over_saved_provider_state() {
|
||||
let request = capture_turn_state_request(
|
||||
"req_use_client_turn_state",
|
||||
Some("turn-same"),
|
||||
Some("turn-same"),
|
||||
Some("client-turn-state"),
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(
|
||||
captured_header_values(&request, "x-codex-turn-state"),
|
||||
vec![b"client-turn-state".to_vec()]
|
||||
);
|
||||
let body = captured_request_body(&request);
|
||||
for key in ["turnState", "turn_state", "x-codex-turn-state"] {
|
||||
assert_eq!(body[key], "client-turn-state");
|
||||
assert_eq!(body["client_metadata"][key], "client-turn-state");
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn new_or_unidentified_turn_should_not_restore_previous_turn_state() {
|
||||
for (request_id, previous_turn_id, current_turn_id, client_turn_state) in [
|
||||
(
|
||||
"req_new_turn_state",
|
||||
Some("turn-old"),
|
||||
Some("turn-new"),
|
||||
Some("stale-client-turn-state"),
|
||||
),
|
||||
(
|
||||
"req_unidentified_turn_state",
|
||||
None,
|
||||
Some("turn-new"),
|
||||
Some("stale-client-turn-state"),
|
||||
),
|
||||
(
|
||||
"req_missing_current_turn_state",
|
||||
Some("turn-old"),
|
||||
None,
|
||||
Some("stale-client-turn-state"),
|
||||
),
|
||||
async fn same_account_continuation_forwards_only_the_current_request_turn_state() {
|
||||
// 旧版会话中可能仍有保存的轮次和 token
|
||||
for (turn, supplied) in [
|
||||
(Some("turn-same"), None),
|
||||
(Some("turn-same"), Some("client-turn-state")),
|
||||
(Some("turn-new"), Some("client-turn-state")),
|
||||
(None, Some("client-turn-state")),
|
||||
] {
|
||||
let request = capture_turn_state_request(
|
||||
request_id,
|
||||
previous_turn_id,
|
||||
current_turn_id,
|
||||
client_turn_state,
|
||||
)
|
||||
.await;
|
||||
assert!(captured_header_values(&request, "x-codex-turn-state").is_empty());
|
||||
let request = capture_turn_state_request(turn, supplied).await;
|
||||
let headers = captured_header_values(&request, "x-codex-turn-state");
|
||||
assert_eq!(
|
||||
headers,
|
||||
supplied
|
||||
.map(|value| value.as_bytes())
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>()
|
||||
);
|
||||
let body = captured_request_body(&request);
|
||||
for key in ["turnState", "turn_state", "x-codex-turn-state"] {
|
||||
assert!(body.get(key).is_none(), "stale body state: {key}");
|
||||
assert!(
|
||||
body["client_metadata"].get(key).is_none(),
|
||||
"stale metadata state: {key}"
|
||||
assert_eq!(body.get(key).and_then(Value::as_str), supplied);
|
||||
assert_eq!(
|
||||
body["client_metadata"].get(key).and_then(Value::as_str),
|
||||
supplied
|
||||
);
|
||||
}
|
||||
assert_eq!(body["input"][0]["content"][0]["text"], "current input");
|
||||
|
||||
@@ -2,6 +2,45 @@
|
||||
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn websocket_body_fields_stay_in_the_body_when_using_http_upstream() {
|
||||
let frame = r#"{"request_kind":"turn","tool_namespaces_info":{"workspace":{}}}"#;
|
||||
let body = json!({
|
||||
"model": "gpt-5.4", "input": "hello",
|
||||
"client_metadata": {"x-codex-turn-metadata": frame},
|
||||
"betaFeatures": "only-in-body", "includeTimingMetrics": "true"
|
||||
});
|
||||
let sent = capture_scoped_http_request(
|
||||
"req_frame_only",
|
||||
"acct_scope_same",
|
||||
"acct_scope_same",
|
||||
body.as_object().unwrap().clone(),
|
||||
Map::from_iter([(
|
||||
gateway_protocol::openai::WEBSOCKET_FRAME_CONTEXT_KEY.into(),
|
||||
json!(true),
|
||||
)]),
|
||||
)
|
||||
.await;
|
||||
for header in [
|
||||
"x-codex-turn-metadata",
|
||||
"x-codex-beta-features",
|
||||
"x-responsesapi-include-timing-metrics",
|
||||
] {
|
||||
assert!(!sent.headers.contains_key(header), "{header}");
|
||||
}
|
||||
let sent_body = captured_request_body(&sent);
|
||||
let sent_frame = sent_body["client_metadata"]["x-codex-turn-metadata"]
|
||||
.as_str()
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
serde_json::from_str::<Value>(sent_frame).unwrap(),
|
||||
serde_json::from_str::<Value>(frame).unwrap()
|
||||
);
|
||||
for key in ["betaFeatures", "includeTimingMetrics"] {
|
||||
assert_eq!(sent_body[key], body[key]);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn responses_options_follow_the_selected_upstream_on_both_transports() {
|
||||
let oauth_omissions = json!({
|
||||
|
||||
@@ -546,6 +546,8 @@ fn decoder_should_use_non_reasoning_preview_web_search_price() {
|
||||
fn gpt_6_decoders_should_use_reasoning_preview_web_search_price() {
|
||||
let tools = vec![json!({ "type": "web_search_preview" })];
|
||||
for model in [
|
||||
"gpt-6.2-astra",
|
||||
"gpt-6.2-sol",
|
||||
"gpt-6-astra",
|
||||
"gpt-6.1-sol",
|
||||
"gpt-6.1-terra",
|
||||
|
||||
@@ -431,8 +431,6 @@ fn context() -> CodexRequestContext<'static> {
|
||||
request_id: "req_catalog",
|
||||
turn_state: None,
|
||||
turn_metadata: None,
|
||||
beta_features: None,
|
||||
include_timing_metrics: None,
|
||||
version: None,
|
||||
codex_window_id: None,
|
||||
parent_thread_id: None,
|
||||
@@ -441,7 +439,6 @@ fn context() -> CodexRequestContext<'static> {
|
||||
session_id: None,
|
||||
thread_id: None,
|
||||
client_request_id: None,
|
||||
turn_id: None,
|
||||
account_selection: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,8 +34,6 @@ fn request_context_debug_should_redact_all_identity_material() {
|
||||
request_id: "req_safe",
|
||||
turn_state: Some("turn-secret-marker"),
|
||||
turn_metadata: Some("metadata-secret-marker"),
|
||||
beta_features: None,
|
||||
include_timing_metrics: None,
|
||||
version: None,
|
||||
codex_window_id: None,
|
||||
parent_thread_id: None,
|
||||
@@ -44,7 +42,6 @@ fn request_context_debug_should_redact_all_identity_material() {
|
||||
session_id: None,
|
||||
thread_id: None,
|
||||
client_request_id: None,
|
||||
turn_id: None,
|
||||
account_selection: Default::default(),
|
||||
};
|
||||
let debug = format!("{context:?}");
|
||||
|
||||
@@ -148,8 +148,15 @@ async fn backend_websocket_should_forward_context_headers_and_preserve_payload_f
|
||||
.expect("send terminal event");
|
||||
payload
|
||||
});
|
||||
let mut request =
|
||||
codex_request_with_prompt_cache_key("gpt-test", "be brief", Vec::new(), "client-thread");
|
||||
let mut body = codex_request_body("gpt-test", "be brief", Vec::new());
|
||||
body.insert("prompt_cache_key".into(), json!("client-thread"));
|
||||
let mut request = encode_request_with_headers(
|
||||
body,
|
||||
&[
|
||||
("x-codex-beta-features", "feature-a"),
|
||||
("x-responsesapi-include-timing-metrics", "true"),
|
||||
],
|
||||
);
|
||||
request.use_websocket = true;
|
||||
request.responses_lite = Some("true".to_owned());
|
||||
request.memgen_request = Some("true".to_owned());
|
||||
@@ -179,8 +186,6 @@ async fn backend_websocket_should_forward_context_headers_and_preserve_payload_f
|
||||
request_id: "req_ws_security",
|
||||
turn_state: Some("turn-state"),
|
||||
turn_metadata: Some("{\"thread_source\":\"subagent\"}"),
|
||||
beta_features: Some("feature-a"),
|
||||
include_timing_metrics: Some("true"),
|
||||
version: Some("26.318.11754"),
|
||||
codex_window_id: Some("cw_derived"),
|
||||
parent_thread_id: Some("parent-456"),
|
||||
@@ -189,7 +194,6 @@ async fn backend_websocket_should_forward_context_headers_and_preserve_payload_f
|
||||
session_id: Some("cp_derived"),
|
||||
thread_id: None,
|
||||
client_request_id: None,
|
||||
turn_id: None,
|
||||
account_selection: Default::default(),
|
||||
},
|
||||
)
|
||||
@@ -265,11 +269,18 @@ async fn backend_http_should_send_codex_context_without_browser_headers() {
|
||||
write_completed_sse_response(&mut stream).await;
|
||||
request
|
||||
});
|
||||
let mut request = codex_request("gpt-test", "", Vec::new());
|
||||
let mut body = codex_request_body("gpt-test", "", Vec::new());
|
||||
body.insert("betaFeatures".into(), json!("body-beta"));
|
||||
body.insert("includeTimingMetrics".into(), json!("body-timing"));
|
||||
let mut request = encode_request_with_headers(
|
||||
body,
|
||||
&[
|
||||
("x-codex-beta-features", "beta-a"),
|
||||
("x-responsesapi-include-timing-metrics", "true"),
|
||||
],
|
||||
);
|
||||
request.force_http_sse = true;
|
||||
request.turn_metadata = Some("turn-meta".to_owned());
|
||||
request.beta_features = Some("beta-a".to_owned());
|
||||
request.include_timing_metrics = Some("true".to_owned());
|
||||
request.version = Some("26.707.51957".to_owned());
|
||||
request.codex_window_id = Some("cw_1".to_owned());
|
||||
request.parent_thread_id = Some("parent-1".to_owned());
|
||||
@@ -297,8 +308,6 @@ async fn backend_http_should_send_codex_context_without_browser_headers() {
|
||||
request_id: "req_order",
|
||||
turn_state: Some("turn-state"),
|
||||
turn_metadata: request.turn_metadata.as_deref(),
|
||||
beta_features: request.beta_features.as_deref(),
|
||||
include_timing_metrics: request.include_timing_metrics.as_deref(),
|
||||
version: request.version.as_deref(),
|
||||
codex_window_id: request.codex_window_id.as_deref(),
|
||||
parent_thread_id: request.parent_thread_id.as_deref(),
|
||||
@@ -307,7 +316,6 @@ async fn backend_http_should_send_codex_context_without_browser_headers() {
|
||||
session_id: Some("session-1"),
|
||||
thread_id: None,
|
||||
client_request_id: None,
|
||||
turn_id: None,
|
||||
account_selection: Default::default(),
|
||||
},
|
||||
)
|
||||
@@ -429,11 +437,8 @@ async fn backend_http_should_ignore_unrepresentable_protocol_headers_without_blo
|
||||
client_request_id: Some("opaque\nclient-request-id"),
|
||||
session_id: Some("opaque\nsession"),
|
||||
thread_id: Some("opaque\nthread"),
|
||||
turn_id: Some("opaque\nturn"),
|
||||
codex_window_id: Some("opaque\nwindow"),
|
||||
parent_thread_id: Some("opaque\nparent"),
|
||||
beta_features: Some("opaque\nbeta"),
|
||||
include_timing_metrics: Some("opaque\ntiming"),
|
||||
version: Some("opaque\nversion"),
|
||||
turn_state: request.turn_state.as_deref(),
|
||||
turn_metadata: request.turn_metadata.as_deref(),
|
||||
|
||||
@@ -130,8 +130,6 @@ async fn codex_backend_client_should_preserve_oversized_error_response() {
|
||||
request_id: "req_large_error",
|
||||
turn_state: None,
|
||||
turn_metadata: None,
|
||||
beta_features: None,
|
||||
include_timing_metrics: None,
|
||||
version: None,
|
||||
codex_window_id: None,
|
||||
parent_thread_id: None,
|
||||
@@ -140,7 +138,6 @@ async fn codex_backend_client_should_preserve_oversized_error_response() {
|
||||
session_id: None,
|
||||
thread_id: None,
|
||||
client_request_id: None,
|
||||
turn_id: None,
|
||||
account_selection: Default::default(),
|
||||
},
|
||||
)
|
||||
@@ -231,8 +228,6 @@ async fn codex_backend_client_should_parse_retry_after_from_rate_limit_error_bod
|
||||
request_id: "req_http_retry_after_body",
|
||||
turn_state: None,
|
||||
turn_metadata: None,
|
||||
beta_features: None,
|
||||
include_timing_metrics: None,
|
||||
version: None,
|
||||
codex_window_id: None,
|
||||
parent_thread_id: None,
|
||||
@@ -241,7 +236,6 @@ async fn codex_backend_client_should_parse_retry_after_from_rate_limit_error_bod
|
||||
session_id: None,
|
||||
thread_id: None,
|
||||
client_request_id: None,
|
||||
turn_id: None,
|
||||
account_selection: Default::default(),
|
||||
},
|
||||
)
|
||||
@@ -417,8 +411,6 @@ async fn codex_backend_client_should_capture_forwardable_response_metadata() {
|
||||
request_id: "req_response_metadata",
|
||||
turn_state: None,
|
||||
turn_metadata: None,
|
||||
beta_features: None,
|
||||
include_timing_metrics: None,
|
||||
version: None,
|
||||
codex_window_id: None,
|
||||
parent_thread_id: None,
|
||||
@@ -427,7 +419,6 @@ async fn codex_backend_client_should_capture_forwardable_response_metadata() {
|
||||
session_id: None,
|
||||
thread_id: None,
|
||||
client_request_id: None,
|
||||
turn_id: None,
|
||||
account_selection: Default::default(),
|
||||
},
|
||||
)
|
||||
|
||||
@@ -157,8 +157,6 @@ pub(crate) fn request_context<'a>(
|
||||
request_id,
|
||||
turn_state: None,
|
||||
turn_metadata: None,
|
||||
beta_features: None,
|
||||
include_timing_metrics: None,
|
||||
version: None,
|
||||
codex_window_id: None,
|
||||
parent_thread_id: None,
|
||||
@@ -167,7 +165,6 @@ pub(crate) fn request_context<'a>(
|
||||
session_id: None,
|
||||
thread_id: None,
|
||||
client_request_id: None,
|
||||
turn_id: None,
|
||||
account_selection: Default::default(),
|
||||
}
|
||||
}
|
||||
@@ -357,6 +354,33 @@ fn codex_request_with_prompt_cache_key(
|
||||
CodexResponsesRequest::from_body(body)
|
||||
}
|
||||
|
||||
fn encode_request_with_headers(
|
||||
body: Map<String, Value>,
|
||||
headers: &[(&str, &str)],
|
||||
) -> CodexResponsesRequest {
|
||||
use base64::{Engine as _, engine::general_purpose::STANDARD};
|
||||
use gateway_core::operation::{GenerateRequest, ProtocolPayload};
|
||||
use provider_openai::transport::request::encode_generate_request;
|
||||
|
||||
let headers: Vec<_> = headers
|
||||
.iter()
|
||||
.map(|(name, value)| json!([name, STANDARD.encode(value)]))
|
||||
.collect();
|
||||
let model = body["model"].as_str().unwrap().to_owned();
|
||||
let payload = ProtocolPayload::json_object("openai", body)
|
||||
.unwrap()
|
||||
.with_context(Map::from_iter([(
|
||||
"opaque_request_headers".to_owned(),
|
||||
json!(headers),
|
||||
)]));
|
||||
encode_generate_request(
|
||||
&GenerateRequest::from_protocol_payload(payload),
|
||||
&model,
|
||||
None,
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn codex_request_body(
|
||||
model: impl Into<String>,
|
||||
instructions: impl Into<String>,
|
||||
|
||||
@@ -542,10 +542,39 @@ fn encoder_should_extract_websocket_context_from_client_metadata() {
|
||||
);
|
||||
assert_eq!(encoded.client_thread_id.as_deref(), Some("metadata-thread"));
|
||||
assert_eq!(encoded.turn_state.as_deref(), Some("metadata-turn-state"));
|
||||
assert_eq!(encoded.client_turn_id.as_deref(), Some("metadata-turn-id"));
|
||||
assert_eq!(encoded.body().get("client_metadata"), Some(&metadata));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn websocket_frame_turn_state_overrides_the_opening_header() {
|
||||
let payload = ProtocolPayload::json_object(
|
||||
"openai",
|
||||
Map::from_iter([
|
||||
("model".to_owned(), json!("client-model")),
|
||||
("input".to_owned(), json!("prompt")),
|
||||
(
|
||||
"client_metadata".to_owned(),
|
||||
json!({"x-codex-turn-state": "frame"}),
|
||||
),
|
||||
]),
|
||||
)
|
||||
.unwrap()
|
||||
.with_context(Map::from_iter([
|
||||
(
|
||||
"downstream_websocket_connection_id".to_owned(),
|
||||
json!("ws_test"),
|
||||
),
|
||||
("turn_state".to_owned(), json!("opening")),
|
||||
]));
|
||||
let encoded = encode_generate_request(
|
||||
&GenerateRequest::from_protocol_payload(payload),
|
||||
"gpt-test",
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(encoded.turn_state.as_deref(), Some("frame"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn header_context_should_win_over_body_topline_aliases() {
|
||||
let payload = ProtocolPayload::json_object(
|
||||
@@ -574,10 +603,6 @@ fn header_context_should_win_over_body_topline_aliases() {
|
||||
"turn_metadata".to_owned(),
|
||||
Value::String("header-turn-metadata".to_owned()),
|
||||
),
|
||||
(
|
||||
"turn_id".to_owned(),
|
||||
Value::String("header-turn-id".to_owned()),
|
||||
),
|
||||
]));
|
||||
let request = GenerateRequest::from_protocol_payload(payload);
|
||||
|
||||
@@ -588,7 +613,6 @@ fn header_context_should_win_over_body_topline_aliases() {
|
||||
encoded.turn_metadata.as_deref(),
|
||||
Some("header-turn-metadata")
|
||||
);
|
||||
assert_eq!(encoded.client_turn_id.as_deref(), Some("header-turn-id"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -82,6 +82,28 @@ fn astra_billing_should_preserve_components_across_tiers_and_context_boundary()
|
||||
fn gpt_6_models_should_use_published_prices_across_tiers_and_context_boundary() {
|
||||
// 每档依次核对每百万 Token 的输入、缓存读取、缓存写入和输出价格
|
||||
for (model, cases) in [
|
||||
(
|
||||
"gpt-6.2-astra",
|
||||
[
|
||||
(None, 272_000, ["8", "0.4", "10", "40"]),
|
||||
(None, 272_001, ["16", "0.8", "20", "60"]),
|
||||
(Some("fast"), 272_000, ["16", "0.8", "20", "80"]),
|
||||
(Some("priority"), 272_001, ["32", "1.6", "40", "120"]),
|
||||
(Some("ultrafast"), 272_000, ["48", "2.4", "60", "240"]),
|
||||
(Some("ultrafast"), 272_001, ["96", "4.8", "120", "360"]),
|
||||
],
|
||||
),
|
||||
(
|
||||
"gpt-6.2-sol",
|
||||
[
|
||||
(None, 272_000, ["2", "0.1", "2.5", "10"]),
|
||||
(None, 272_001, ["4", "0.2", "5", "15"]),
|
||||
(Some("fast"), 272_000, ["4", "0.2", "5", "20"]),
|
||||
(Some("priority"), 272_001, ["8", "0.4", "10", "30"]),
|
||||
(Some("ultrafast"), 272_000, ["12", "0.6", "15", "60"]),
|
||||
(Some("ultrafast"), 272_001, ["24", "1.2", "30", "90"]),
|
||||
],
|
||||
),
|
||||
(
|
||||
"gpt-6.1-sol",
|
||||
[
|
||||
@@ -457,6 +479,10 @@ fn billing_should_not_inherit_prices_for_unknown_models_or_tiers() {
|
||||
("gpt-5.5-cyber", 272_001, None),
|
||||
("gpt-5.6-cyber", 1, Some("fast")),
|
||||
("gpt-5.3-codex", 1, Some("flex")),
|
||||
("gpt-6.2-astra", 1, Some("flex")),
|
||||
("gpt-6.2-astra", 272_001, Some("flex")),
|
||||
("gpt-6.2-sol", 1, Some("flex")),
|
||||
("gpt-6.2-sol", 272_001, Some("flex")),
|
||||
("gpt-6-astra", 1, Some("auto")),
|
||||
("gpt-6-astra", 1, Some("future")),
|
||||
("gpt-6.1-terra", 1, Some("ultrafast")),
|
||||
@@ -516,8 +542,6 @@ async fn fetch_should_use_wham_usage_headers_only() {
|
||||
request_id: "req_usage_headers",
|
||||
turn_state: Some("turn-state"),
|
||||
turn_metadata: Some("turn-meta"),
|
||||
beta_features: Some("feature-a"),
|
||||
include_timing_metrics: Some("true"),
|
||||
version: Some("26.318.11754"),
|
||||
codex_window_id: Some("cw_1"),
|
||||
parent_thread_id: Some("parent-1"),
|
||||
@@ -526,7 +550,6 @@ async fn fetch_should_use_wham_usage_headers_only() {
|
||||
session_id: Some("session-1"),
|
||||
thread_id: Some("thread-1"),
|
||||
client_request_id: Some("client-request-1"),
|
||||
turn_id: Some("turn-1"),
|
||||
account_selection: Default::default(),
|
||||
})
|
||||
.await
|
||||
@@ -917,8 +940,6 @@ fn context() -> CodexRequestContext<'static> {
|
||||
request_id: "req_usage_limit",
|
||||
turn_state: None,
|
||||
turn_metadata: None,
|
||||
beta_features: None,
|
||||
include_timing_metrics: None,
|
||||
version: None,
|
||||
codex_window_id: None,
|
||||
parent_thread_id: None,
|
||||
@@ -927,7 +948,6 @@ fn context() -> CodexRequestContext<'static> {
|
||||
session_id: None,
|
||||
thread_id: None,
|
||||
client_request_id: None,
|
||||
turn_id: None,
|
||||
account_selection: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
//! 验证 Codex WebSocket 线协议指纹
|
||||
|
||||
use std::io::Read as _;
|
||||
|
||||
use base64::{Engine as _, engine::general_purpose::STANDARD};
|
||||
use flate2::{Decompress, FlushDecompress};
|
||||
use flate2::read::DeflateDecoder;
|
||||
use tokio_tungstenite::tungstenite::handshake::derive_accept_key;
|
||||
|
||||
use super::*;
|
||||
@@ -16,42 +18,7 @@ struct ClientFrame {
|
||||
|
||||
#[tokio::test]
|
||||
async fn official_websocket_wire_fingerprint_should_remain_aligned() {
|
||||
let listener = TcpListener::bind("127.0.0.1:0")
|
||||
.await
|
||||
.expect("bind fingerprint server");
|
||||
let address = listener.local_addr().expect("fingerprint server address");
|
||||
let server = tokio::spawn(async move {
|
||||
let (mut stream, _) = listener.accept().await.expect("accept fingerprint client");
|
||||
let opening = read_http_request(&mut stream).await;
|
||||
let websocket_key = read_header_value(&opening, "sec-websocket-key")
|
||||
.expect("WebSocket key")
|
||||
.to_owned();
|
||||
let response = format!(
|
||||
concat!(
|
||||
"HTTP/1.1 101 Switching Protocols\r\n",
|
||||
"Connection: Upgrade\r\n",
|
||||
"Upgrade: websocket\r\n",
|
||||
"Sec-WebSocket-Accept: {}\r\n",
|
||||
"Sec-WebSocket-Extensions: permessage-deflate\r\n",
|
||||
"\r\n"
|
||||
),
|
||||
derive_accept_key(websocket_key.as_bytes())
|
||||
);
|
||||
stream
|
||||
.write_all(response.as_bytes())
|
||||
.await
|
||||
.expect("write upgrade response");
|
||||
|
||||
let frame = read_client_frame(&mut stream).await;
|
||||
write_server_text_frame(
|
||||
&mut stream,
|
||||
&completed_websocket_response("resp_wire_fingerprint", 1, 1),
|
||||
)
|
||||
.await;
|
||||
(opening, websocket_key, frame)
|
||||
});
|
||||
|
||||
let mut request = CodexResponsesRequest::from_body(
|
||||
let mut request = encode_request_with_headers(
|
||||
json!({
|
||||
"model": "gpt-5.5",
|
||||
"instructions": "be brief",
|
||||
@@ -70,44 +37,28 @@ async fn official_websocket_wire_fingerprint_should_remain_aligned() {
|
||||
.as_object()
|
||||
.expect("request object")
|
||||
.clone(),
|
||||
&[("x-codex-beta-features", "feature-a")],
|
||||
);
|
||||
request.use_websocket = true;
|
||||
request.local_conversation_id = Some("conversation-fingerprint".to_owned());
|
||||
request.turn_metadata = Some(r#"{"kind":"fingerprint"}"#.to_owned());
|
||||
request.beta_features = Some("feature-a".to_owned());
|
||||
request.version = Some("26.908.40834".to_owned());
|
||||
request.codex_window_id = Some("window-fingerprint".to_owned());
|
||||
let client = CodexBackendClient::new(
|
||||
reqwest::Client::builder()
|
||||
.no_proxy()
|
||||
.build()
|
||||
.expect("HTTP client"),
|
||||
format!("http://{address}/backend-api"),
|
||||
test_wire_profile(),
|
||||
)
|
||||
.with_websocket_pool(Arc::new(CodexWebSocketPool::new(Duration::from_mins(1))));
|
||||
|
||||
let result = client
|
||||
.create_response(
|
||||
&request,
|
||||
CodexRequestContext {
|
||||
response_control: None,
|
||||
trace: None,
|
||||
turn_metadata: request.turn_metadata.as_deref(),
|
||||
beta_features: request.beta_features.as_deref(),
|
||||
version: request.version.as_deref(),
|
||||
codex_window_id: request.codex_window_id.as_deref(),
|
||||
session_id: Some("session-fingerprint"),
|
||||
thread_id: Some("thread-fingerprint"),
|
||||
client_request_id: Some("request-fingerprint"),
|
||||
installation_id: Some("3b11bc82-c666-417d-b9ff-2a34fd040385"),
|
||||
..request_context("req_wire_fingerprint", Some("account-fingerprint"))
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
result.unwrap_or_else(|error| panic!("fingerprint WebSocket response: {error}"));
|
||||
let (opening, websocket_key, frame) = server.await.expect("fingerprint server task");
|
||||
let context = CodexRequestContext {
|
||||
turn_metadata: request.turn_metadata.as_deref(),
|
||||
version: request.version.as_deref(),
|
||||
codex_window_id: request.codex_window_id.as_deref(),
|
||||
session_id: Some("session-fingerprint"),
|
||||
thread_id: Some("thread-fingerprint"),
|
||||
client_request_id: Some("request-fingerprint"),
|
||||
installation_id: Some("3b11bc82-c666-417d-b9ff-2a34fd040385"),
|
||||
..request_context("req_wire_fingerprint", Some("account-fingerprint"))
|
||||
};
|
||||
let (opening, frames) = capture_response_create(&request, context, true).await;
|
||||
let [frame] = frames.as_slice() else {
|
||||
panic!("expected a single text frame")
|
||||
};
|
||||
let websocket_key = read_header_value(&opening, "sec-websocket-key").unwrap();
|
||||
assert_eq!(
|
||||
opening.lines().next(),
|
||||
Some("GET /backend-api/codex/responses HTTP/1.1")
|
||||
@@ -207,6 +158,121 @@ async fn official_websocket_wire_fingerprint_should_remain_aligned() {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn large_responses_use_continuation_frames_for_one_text_message() {
|
||||
const MIB: usize = 1024 * 1024;
|
||||
let make_request = |instructions: &str| {
|
||||
let mut request = codex_request("gpt-test", instructions, Vec::new());
|
||||
request.use_websocket = true;
|
||||
request.local_conversation_id = Some("fragment-test".to_owned());
|
||||
request
|
||||
};
|
||||
let context = request_context("fragment-test", Some("account-test"));
|
||||
|
||||
// 用实际序列化的空指令测量 JSON 开销,边界包含元数据
|
||||
let (_, baseline) = capture_response_create(&make_request(""), context, false).await;
|
||||
let empty_payload = std::str::from_utf8(&baseline[0].payload).unwrap();
|
||||
let instructions_key = r#""instructions":""#;
|
||||
let start = empty_payload.find(instructions_key).unwrap() + instructions_key.len();
|
||||
// FIN 只在末帧,文本操作码和压缩标记只在首帧
|
||||
for (size, deflate, headers) in [
|
||||
(MIB, false, &[0x81][..]),
|
||||
(MIB + 1, false, &[0x01, 0x80]),
|
||||
(MIB + 1, true, &[0x41, 0x80]),
|
||||
(2 * MIB + 1, true, &[0x41, 0x00, 0x80]),
|
||||
] {
|
||||
let mut instructions = "a".repeat(size - empty_payload.len());
|
||||
if size > 2 * MIB {
|
||||
// 分帧边界穿过一个 UTF-8 字符
|
||||
instructions.replace_range(MIB - start - 1..MIB - start + 3, "🌱");
|
||||
}
|
||||
let request = make_request(&instructions);
|
||||
let (_, frames) = capture_response_create(&request, context, deflate).await;
|
||||
let actual_headers: Vec<_> = frames.iter().map(|frame| frame.first_byte).collect();
|
||||
assert_eq!(actual_headers, headers);
|
||||
let mut payload = Vec::new();
|
||||
for (index, frame) in frames.iter().enumerate() {
|
||||
assert!(frame.masked);
|
||||
if !deflate {
|
||||
assert_eq!(frame.payload.len(), (size - index * MIB).min(MIB));
|
||||
}
|
||||
payload.extend_from_slice(&frame.payload);
|
||||
}
|
||||
if deflate {
|
||||
payload = decompress_permessage_deflate(&payload);
|
||||
}
|
||||
assert_eq!(payload.len(), size);
|
||||
let decoded: Value = serde_json::from_slice(&payload).unwrap();
|
||||
assert!(decoded["instructions"].as_str() == Some(&instructions));
|
||||
}
|
||||
}
|
||||
|
||||
async fn capture_response_create(
|
||||
request: &CodexResponsesRequest,
|
||||
context: CodexRequestContext<'_>,
|
||||
deflate: bool,
|
||||
) -> (String, Vec<ClientFrame>) {
|
||||
let listener = TcpListener::bind("127.0.0.1:0")
|
||||
.await
|
||||
.expect("bind fingerprint server");
|
||||
let address = listener.local_addr().expect("fingerprint server address");
|
||||
let server = tokio::spawn(async move {
|
||||
let (mut stream, _) = listener.accept().await.expect("accept fingerprint client");
|
||||
let opening = read_http_request(&mut stream).await;
|
||||
let websocket_key =
|
||||
read_header_value(&opening, "sec-websocket-key").expect("WebSocket key");
|
||||
let extension = if deflate {
|
||||
"Sec-WebSocket-Extensions: permessage-deflate\r\n"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let response = format!(
|
||||
concat!(
|
||||
"HTTP/1.1 101 Switching Protocols\r\n",
|
||||
"Connection: Upgrade\r\n",
|
||||
"Upgrade: websocket\r\n",
|
||||
"Sec-WebSocket-Accept: {}\r\n",
|
||||
"{}",
|
||||
"\r\n"
|
||||
),
|
||||
derive_accept_key(websocket_key.as_bytes()),
|
||||
extension
|
||||
);
|
||||
stream
|
||||
.write_all(response.as_bytes())
|
||||
.await
|
||||
.expect("write upgrade response");
|
||||
|
||||
let mut frames = Vec::new();
|
||||
loop {
|
||||
let frame = read_client_frame(&mut stream).await;
|
||||
let finished = frame.first_byte & 0x80 != 0;
|
||||
frames.push(frame);
|
||||
if finished {
|
||||
break;
|
||||
}
|
||||
}
|
||||
write_server_text_frame(
|
||||
&mut stream,
|
||||
&completed_websocket_response("resp_wire_fingerprint", 1, 1),
|
||||
)
|
||||
.await;
|
||||
(opening, frames)
|
||||
});
|
||||
|
||||
let client = CodexBackendClient::new(
|
||||
reqwest::Client::builder().no_proxy().build().unwrap(),
|
||||
format!("http://{address}/backend-api"),
|
||||
test_wire_profile(),
|
||||
)
|
||||
.with_websocket_pool(Arc::new(CodexWebSocketPool::new(Duration::from_mins(1))));
|
||||
client
|
||||
.create_response(request, context)
|
||||
.await
|
||||
.expect("fingerprint WebSocket response");
|
||||
server.await.expect("fingerprint server task")
|
||||
}
|
||||
|
||||
async fn read_client_frame(stream: &mut TcpStream) -> ClientFrame {
|
||||
let first_byte = stream.read_u8().await.expect("frame first byte");
|
||||
let second_byte = stream.read_u8().await.expect("frame second byte");
|
||||
@@ -240,17 +306,13 @@ async fn read_client_frame(stream: &mut TcpStream) -> ClientFrame {
|
||||
}
|
||||
|
||||
fn decompress_permessage_deflate(payload: &[u8]) -> Vec<u8> {
|
||||
let mut compressed = Vec::with_capacity(payload.len() + 4);
|
||||
compressed.extend_from_slice(payload);
|
||||
compressed.extend_from_slice(&[0x00, 0x00, 0xff, 0xff]);
|
||||
let mut output = vec![0_u8; 64 * 1024];
|
||||
let mut decompressor = Decompress::new(false);
|
||||
decompressor
|
||||
.decompress(&compressed, &mut output, FlushDecompress::Sync)
|
||||
.expect("decompress response.create frame");
|
||||
output.truncate(
|
||||
usize::try_from(decompressor.total_out()).expect("decompressed length fits usize"),
|
||||
);
|
||||
let mut compressed = payload.to_vec();
|
||||
// 补回 permessage-deflate 的同步刷新尾部,再加空终止块供完整流解码器读取
|
||||
compressed.extend_from_slice(&[0, 0, 0xff, 0xff, 1, 0, 0, 0xff, 0xff]);
|
||||
let mut output = Vec::new();
|
||||
DeflateDecoder::new(compressed.as_slice())
|
||||
.read_to_end(&mut output)
|
||||
.unwrap();
|
||||
output
|
||||
}
|
||||
|
||||
|
||||
@@ -2288,8 +2288,6 @@ async fn codex_backend_client_should_use_websocket_when_previous_response_id_is_
|
||||
request_id: "req_ws_client",
|
||||
turn_state: None,
|
||||
turn_metadata: None,
|
||||
beta_features: None,
|
||||
include_timing_metrics: None,
|
||||
version: None,
|
||||
codex_window_id: None,
|
||||
parent_thread_id: None,
|
||||
@@ -2298,7 +2296,6 @@ async fn codex_backend_client_should_use_websocket_when_previous_response_id_is_
|
||||
session_id: None,
|
||||
thread_id: None,
|
||||
client_request_id: None,
|
||||
turn_id: None,
|
||||
account_selection: Default::default(),
|
||||
},
|
||||
)
|
||||
|
||||
+4
-2
@@ -223,6 +223,8 @@ docker compose -f deploy/compose.yaml stats --no-stream
|
||||
|
||||
## 部署结构
|
||||
|
||||
PostgreSQL 最低版本为 16,默认 Compose 使用 PostgreSQL 18
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
Client[客户端 / 管理浏览器] -->|HTTPS| Proxy[反向代理]
|
||||
@@ -320,7 +322,7 @@ store:
|
||||
## Windows 二进制部署
|
||||
|
||||
发行包提供 Windows x64(`windows_amd64`)和 ARM64(`windows_arm64`)版本,按运行机器的 CPU 架构选择。
|
||||
需先准备可访问的 PostgreSQL 和 Redis,创建数据库及用户;使用数据库备份功能时,还需将匹配服务端版本的 `pg_dump` 加入 `PATH`
|
||||
需先按[数据库版本要求](#部署结构)准备 PostgreSQL,创建数据库及用户,并确保 Redis 可访问。使用数据库备份功能时,还需将匹配服务端版本的 `pg_dump` 加入 `PATH`
|
||||
|
||||
从 [Release 附件](https://github.com/zyycn/codex-proxy-rs/releases) 下载对应的
|
||||
`codex-proxy-rs_<version>_windows_<arch>.tar.gz`,解压后保留 `codex-proxy-rs.exe`、`web/dist`、`deploy` 和 `plugins/official` 的目录关系。
|
||||
@@ -964,7 +966,7 @@ exp 不进入正式版、alpha、beta 或 rc,普通实例也不能选择 Exp
|
||||
其他通道的版本及发布说明,手动检查显示“当前没有可用更新”。检查失败单独报告,不能当作没有更新
|
||||
|
||||
alpha、beta、rc、exp 在 GitHub 标记为 Pre-release,不覆盖 GitHub Latest 或镜像 `latest`。
|
||||
升级前核对目标版本的数据库要求;可连续升级的发行线与约束见[迁移冻结规则](../backend/migrations/README.md#冻结规则)
|
||||
升级前确认 PostgreSQL 满足目标版本的[部署要求](#部署结构)。可连续升级的发行线与约束见[迁移冻结规则](../backend/migrations/README.md#冻结规则)
|
||||
|
||||
### 管理端在线更新
|
||||
|
||||
|
||||
+16
-9
@@ -542,9 +542,13 @@ Images 的 `x-codex-image-turn-id` 和 Desktop 自动 Git 上下文的 `clientUs
|
||||
续接 response ID、历史 item/call ID、业务正文和未知扩展不参与映射,
|
||||
该处理减少显式追踪标识的跨账号复用,不保证会话内容或业务 metadata 无法关联
|
||||
|
||||
`x-codex-turn-state` 是上游返回的轮次路由 token,同一轮内应原样回传,开始新轮次时清除。
|
||||
全量请求没有 `previous_response_id` 或缺少本地归属记录时,代理原样保留客户端回传值;
|
||||
明确换账号或可信续接状态表明不属于同一轮时清除。该 token 不使其他账号状态字段或未知 `previous_response_id` 成为可信续接上下文
|
||||
`x-codex-turn-metadata` 请求头与 `client_metadata` 内的同名字段分别按账号处理并转发。
|
||||
WS 握手的轮次元数据头只随首次请求转发,后续帧在正文中携带本轮元数据。网关优先按当前帧识别普通对话和压缩请求。
|
||||
`x-codex-beta-features` 和 `x-responsesapi-include-timing-metrics` 按原始请求头透传
|
||||
|
||||
`x-codex-turn-state` 是上游签发的轮次路由 token。客户端在同一轮原样回传,新轮次省略。
|
||||
网关转发当前请求提供的值,明确换账号时清除。WS 握手携带的值只用于首次请求,当前帧提供的值优先。
|
||||
续接 response ID 及其他账号状态的归属独立校验
|
||||
|
||||
#### 正文兼容
|
||||
|
||||
@@ -648,7 +652,8 @@ OAuth 账号默认 `prefer_websocket`,客户端使用 HTTP/SSE 时仍可能选
|
||||
客户端可跳过预热继续正式请求,外部插件 adapter 按各自的协议能力处理
|
||||
|
||||
OpenAI Responses 上游 WS 按握手响应头 `x-codex-websocket-max-message-bytes` 声明的上限,
|
||||
检查最终 `response.create` 文本的 UTF-8 字节数。超过上限时,新链、持久化续接和未知外部 previous ID
|
||||
检查最终 `response.create` 文本的 UTF-8 字节数。允许发送的文本超过 1 MiB 时,每 1 MiB 分一帧,接收方仍收到一条消息。
|
||||
超过握手声明的消息上限时,新链、持久化续接和未知外部 previous ID
|
||||
在上传正文前改用 HTTP/SSE;等于上限仍可发送。OAuth 和 API Key 账号、HTTP 和 WS 入站均适用。
|
||||
该上限只属于当前上游连接;响应头缺失或无效时不预判超限。显式 warmup 保持 WS 合同,超限时
|
||||
返回 `websocket_message_too_large`,不重复发送或改用 HTTP
|
||||
@@ -1311,7 +1316,8 @@ OpenAI OAuth 账号的 `capabilities.quotaSettingsSupported` 为真。通过 `PO
|
||||
|
||||
账号视图和 Dashboard 账号概览中的 `planType` 保留原始套餐值;`planTypeDisplay` 使用 Provider 提供的最终名称,
|
||||
前端直接展示。OpenAI 的 `prolite`、`pro`、`promax` 分别显示为 `ProLite`、`Pro`、`ProMax`;
|
||||
其余已知套餐使用官方名称,未知 OpenAI 套餐保留原值。
|
||||
其余已知 OpenAI 套餐使用 Codex `/status` 的名称,例如 `self_serve_business_prolite` 显示为 `Business Premium`,
|
||||
`self_serve_business_usage_based` 显示为 `Business`。未知 OpenAI 套餐保留原值。
|
||||
展示名称保留空格和括号,未提供专属名称规则的 Provider 使用通用大驼峰格式。
|
||||
OpenAI 主动额度刷新和正常响应携带的明确套餐会同步到账号,支持升级与降级;
|
||||
空值或 `unknown` 不覆盖已有套餐,同族泛化值(如 `team`)保留已知的具体套餐子类型。
|
||||
@@ -2420,7 +2426,8 @@ Token,范围 0~1000000、最多四位小数。`"0"` 表示免费,缺少整
|
||||
|
||||
`bands` 可用键为 `standard`、`fast`、`ultrafast`、`flex`、`long_standard`、`long_fast`、`long_ultrafast`、`long_flex`、`image`。
|
||||
OpenAI 长上下文为输入超过 272000 Token,xAI 为输入达到 200000 Token;仍按 Provider 支持的
|
||||
模型与服务档位判断是否能估算。OpenAI 内置 Ultrafast 价目覆盖 `gpt-6-astra` 与 `gpt-6.1-sol`。
|
||||
模型与服务档位判断是否能估算。OpenAI 内置 Ultrafast 价目覆盖 `gpt-6-astra`、`gpt-6.1-sol`、
|
||||
`gpt-6.2-astra` 和 `gpt-6.2-sol`,均按对应上下文标准价的 6 倍估算。
|
||||
xAI 不接受 `flex`、`long_flex`、`ultrafast`、`long_ultrafast`、`image`。图像端点的 `standard`
|
||||
用于文本输入,`image` 用于图像 Token;不能用文本输出单价替代图像输出单价。用量无法完整拆分时不估算
|
||||
|
||||
@@ -2789,7 +2796,7 @@ Dashboard 的 `accountUsage[]` 返回稳定指标 `metric=requests|tokens`、可
|
||||
Dashboard 默认 `period=today`,用量、诊断和错误查询默认 `period=7d`,范围参数遵循[页面时间合同](#页面时间合同)。
|
||||
Dashboard 返回原始 `asOf` 及 `asOfDisplay`,作为当前数据的查询锚点
|
||||
|
||||
请求明细列表接受 `currentPage`、`pageSize`、时间范围、`provider`、`clientApiKeyName`、`accountName`、`model` 和 `status`。
|
||||
请求明细列表接受 `currentPage`、`pageSize`、时间范围、`provider`、`clientApiKeyName`、`accountSearch`、`model` 和 `status`。
|
||||
汇总和洞察接受时间范围与 `provider`;性能和成本另要求 `metric`,诊断另接受 `dimension`、`model` 和 `statusCode`。
|
||||
各接口拒绝未知参数,明细筛选不改变顶部统计范围
|
||||
|
||||
@@ -2859,8 +2866,8 @@ HTTP SSE 请求可能在返回 HTTP 200 后流内失败;WebSocket 已升级时
|
||||
|
||||
管理端请求列表与详情保留 `requestedModel`(客户端请求)与 `upstreamModel`(网关发送)
|
||||
|
||||
请求明细的 `clientApiKeyName`、`accountName` 按字面量包含匹配请求保存的历史名称快照,区分大小写,
|
||||
不解析为 ID,也不依赖当前配置是否存在。`model` 按字面量包含匹配请求模型或上游模型,`%`、`_` 和 `\` 按普通字符处理
|
||||
请求明细的 `clientApiKeyName` 匹配 Key 名称,`accountSearch` 匹配账号名称或邮箱。
|
||||
这些筛选仅使用历史快照,可与时间、状态等条件组合。`model` 匹配请求模型或上游模型。以上文本筛选均按区分大小写的字面量包含匹配,`%`、`_` 和 `\` 按普通字符处理
|
||||
|
||||
运维错误列表的 `search` 使用字面量包含匹配,`%`、`_` 和 `\` 按普通字符处理,不支持密钥值或可见前缀搜索。
|
||||
请求详情通过 `/api/admin/usage/records/detail?id=...` 查询
|
||||
|
||||
@@ -424,7 +424,8 @@ sequenceDiagram
|
||||
|
||||
模型请求默认不限制总执行时长;插件显式设置的总时限从请求开始计时,Provider 的传输空闲超时独立生效。
|
||||
Client Key 并发占用、账号调度槽位与请求恢复记录使用可续期租约,不把租约 TTL 当作总执行预算。
|
||||
会话结束或 Drop 后停止续期;Redis 并发租约丢失或无法在有效期内续期时取消请求,避免失去并发约束后继续执行
|
||||
账号调度槽位随上游流释放。流返回错误、读到 EOF 或网关终止读取时即触发归还,费用结算与请求记录可继续完成。
|
||||
Redis 并发租约丢失或无法在有效期内续期时取消请求,避免失去并发约束后继续执行
|
||||
|
||||
Client Key 鉴权完成后,API adapter 从有界请求头识别客户端,Core 使用请求冻结的 `RuntimeSnapshot`
|
||||
检查最低版本,在进入 Provider 前完成门禁。请求头优先级、远程客户端识别与错误合同见[鉴权约定](api.md#1-鉴权与公共约定)
|
||||
@@ -546,9 +547,8 @@ OpenAI Provider 按 Client Key、所选 OAuth 账号和父响应 ID 关联 WS
|
||||
连接池按快照身份隔离审查连接;复用父连接不会重新计算 Cookie 有效期,Cookie 到期不强制关闭已建立的审查连接
|
||||
|
||||
OpenAI OAuth 的连接、续接状态、Guardian 路由 Cookie 和头像缓存按本地账号、上游用户与工作区隔离,同号令牌续期不改变归属。
|
||||
OpenAI Provider 统一处理 Responses 的账号归属与 turn state 隔离,HTTP 与 WS 消费同一出站请求。
|
||||
全量请求缺少本地归属记录时保留客户端回传的路由 token,明确换号或可信续接状态不属于同一轮时清除;
|
||||
turn state 不参与选号,也不授权其他账号状态或 response ID 的复用
|
||||
Responses 的轮次路由 token 由客户端回传,OpenAI Provider 只在明确换号时清除。
|
||||
选号和 response ID 的归属校验独立于该 token
|
||||
|
||||
### 账号协议与字段适配
|
||||
|
||||
@@ -981,7 +981,8 @@ PostgreSQL schema 由迁移目录按编号管理;冻结、升级与校验规
|
||||
### 账号模型目录
|
||||
|
||||
OpenAI 在 PostgreSQL 保存账号最近一次成功的模型目录、凭据版本、观测时间及 ETag。启动时批量恢复,按不同正文解析一次,保留每个账号的独立归属。没有历史目录也能发布路由快照;能力读取与推理不等待上游查询。
|
||||
后台刷新缺少目录、距上次成功观测已满 30 分钟或收到新 ETag 的账号。刷新期间及失败后继续使用上次成功的目录;凭据或套餐变化后不沿用旧目录。
|
||||
账号缺少目录或收到新 ETag 时,后台重新查询该账号。正常响应的 ETag 与该账号当前凭据和套餐的目录一致时,在内存中续期。距上次成功拉取或确认满 1 小时后,由后台复查。重启后从持久化的目录观测时间起算。
|
||||
刷新期间及失败后继续使用上次成功的目录。凭据或套餐变化后重新发现模型。
|
||||
刷新并发与节奏由 [Provider 目录实现](../backend/crates/providers/openai/src/credential/catalog/mod.rs)统一维护
|
||||
|
||||
OpenAI 从账号目录生成原生列表;先按冻结政策选择来源,再读取本地账号状态核对凭据版本。同一份正文可以复用,但不会把一个账号的模型授予同套餐的其他账号。客户端版本和 Key 身份不触发额外发现
|
||||
@@ -1229,8 +1230,7 @@ observation、调用 metadata;最终失败的关联头不与 opening 身份混
|
||||
这一冷流约束同样覆盖 attempt 中间件的响应读取,Core 校验元数据并登记 attempt 后才允许开始读取。
|
||||
不能因首写失败或缺少 metadata 而伪造零尝试、账号、传输或 `not_sent`
|
||||
|
||||
Provider 流返回的错误、随附事件与重试决策由 Core 会话持有;取消单次等待只暂停该错误的处理,
|
||||
不能因等待取消而重复读取上游或重建 attempt
|
||||
Provider 流返回错误时,Core 会话保留原始错误、随附事件与重试决策,取消单次等待后可继续处理同一次失败
|
||||
|
||||
终态观测写入由会话持有;事件等待被取消后,后续 poll 或 detached finalizer 继续同一个 future,
|
||||
保留原结果及完成时间,不重复创建请求。观测写入失败仍为 best-effort,不阻断费用结算与准入释放。
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
autoInstallPeers: false
|
||||
|
||||
# picomatch 只在构建工具链中使用,未进入发布产物。上游发布修复后升级并移除此例外
|
||||
audit:
|
||||
ignore:
|
||||
- GHSA-fc5q-39xc-vjpf
|
||||
|
||||
# Git 依赖通过 prepack 生成产物;信任自有 UI 仓库,实际提交仍由锁文件固定。
|
||||
allowBuilds:
|
||||
'@codex-proxy/ui@git+https://github.com/zyycn/codex-proxy-ui.git': true
|
||||
|
||||
@@ -425,7 +425,7 @@ interface PageQuery {
|
||||
|
||||
type UsagePageQuery = UsageRangeQuery & PageQuery & {
|
||||
clientApiKeyName?: string
|
||||
accountName?: string
|
||||
accountSearch?: string
|
||||
model?: string
|
||||
status?: UsageRecordStatus
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ const props = withDefaults(
|
||||
planTypeDisplay: string
|
||||
count?: number
|
||||
size?: 'xs' | 'sm' | 'md'
|
||||
wrap?: boolean
|
||||
}>(),
|
||||
{
|
||||
planType: null,
|
||||
@@ -43,13 +44,11 @@ const displayText = computed(() => props.authenticationKind === 'api_key'
|
||||
? 'API'
|
||||
: props.planTypeDisplay)
|
||||
|
||||
const sizeClass = computed(() => {
|
||||
if (props.size === 'xs')
|
||||
return 'h-4.5 rounded-full px-1.5 text-[10px] font-bold'
|
||||
return props.size === 'sm'
|
||||
? 'h-5 rounded-full px-1.75 text-z-xs font-bold'
|
||||
: 'h-5.5 rounded-full px-2 text-z-xs font-heavy'
|
||||
})
|
||||
const sizes = {
|
||||
xs: { text: 'text-[10px] font-bold', singleLine: 'h-4.5 px-1.5' },
|
||||
sm: { text: 'text-z-xs font-bold', singleLine: 'h-5 px-1.75' },
|
||||
md: { text: 'text-z-xs font-heavy', singleLine: 'h-5.5 px-2' },
|
||||
}
|
||||
|
||||
const paletteClass = computed(() => {
|
||||
const key = rawPlanType.value.toLowerCase()
|
||||
@@ -65,11 +64,16 @@ const paletteClass = computed(() => {
|
||||
<AccountUpstreamBadge v-if="upstreamPeer || upstream?.discovered" :size="size" />
|
||||
<span
|
||||
v-else
|
||||
class="inline-flex min-w-0 max-w-full items-center justify-center gap-1 whitespace-nowrap leading-none shadow-z-tertiary"
|
||||
:class="[sizeClass, paletteClass]"
|
||||
class="inline-flex min-w-0 max-w-full items-center justify-center gap-1 shadow-z-tertiary"
|
||||
:class="[
|
||||
sizes[size].text, paletteClass,
|
||||
wrap
|
||||
? 'min-h-5.5 rounded-lg px-1 py-0.75 text-center leading-3.5'
|
||||
: [sizes[size].singleLine, 'rounded-full whitespace-nowrap leading-none'],
|
||||
]"
|
||||
:title="displayText || undefined"
|
||||
>
|
||||
<span class="truncate">{{ displayText }}</span>
|
||||
<span :class="wrap ? 'line-clamp-3 text-balance wrap-anywhere whitespace-normal' : 'truncate'">{{ displayText }}</span>
|
||||
<span v-if="count !== undefined" class="shrink-0 tabular-nums">{{ count }}</span>
|
||||
</span>
|
||||
</template>
|
||||
|
||||
@@ -9,12 +9,22 @@ import UsageDetailPopover from '@/components/usage/UsageDetailPopover.vue'
|
||||
import { usageBillingText } from './shared/presenter'
|
||||
|
||||
const props = defineProps<{
|
||||
record: Pick<UsageListRecord, 'billing'>
|
||||
record: Pick<UsageListRecord, 'billing'> & Partial<Pick<UsageListRecord, 'serviceTier'>>
|
||||
}>()
|
||||
|
||||
const { t } = useI18n({ useScope: 'global' })
|
||||
|
||||
const billing = computed(() => props.record.billing)
|
||||
const speedMode = computed(() => {
|
||||
const tier = props.record.serviceTier ?? billing.value?.serviceTierDisplay.toLowerCase()
|
||||
if (tier === 'fast' || tier === 'priority')
|
||||
return 'Fast'
|
||||
if (tier === 'ultrafast')
|
||||
return 'Ultrafast'
|
||||
return null
|
||||
})
|
||||
const speedModeLabel = computed(() => t('usage.billing.speedMode', { mode: speedMode.value }))
|
||||
|
||||
const billingItems = computed(() => {
|
||||
const value = billing.value
|
||||
if (!value)
|
||||
@@ -137,17 +147,19 @@ function itemValueClass(tone?: string, accent?: boolean) {
|
||||
{{ usageBillingText(record) }}
|
||||
</span>
|
||||
|
||||
<div v-if="billing" class="flex items-center gap-1">
|
||||
<div v-if="billing || speedMode" class="flex shrink-0 items-center gap-1">
|
||||
<span
|
||||
v-if="billing.serviceTierDisplay === 'Fast'"
|
||||
class="inline-flex size-4 items-center justify-center rounded-full bg-z-warning-container text-z-warning-on-container"
|
||||
:title="t('usage.billing.fastMode')"
|
||||
v-if="speedMode"
|
||||
class="inline-flex h-4 items-center rounded-full bg-z-warning-container px-0.5 text-z-warning-on-container"
|
||||
:title="speedModeLabel"
|
||||
role="img"
|
||||
:aria-label="t('usage.billing.fastMode')"
|
||||
:aria-label="speedModeLabel"
|
||||
>
|
||||
<Zap class="size-3" stroke-width="2.2" aria-hidden="true" />
|
||||
<Zap v-if="speedMode === 'Ultrafast'" class="size-3" stroke-width="2.2" aria-hidden="true" />
|
||||
</span>
|
||||
<UsageDetailPopover
|
||||
v-if="billing"
|
||||
:title="billing.longContextBillingApplied ? t('usage.billing.longContextBillingDetails') : t('usage.billing.billingDetails')"
|
||||
:trigger-label="billing.longContextBillingApplied ? t('usage.billing.viewLongContextBillingDetails') : t('usage.billing.viewCostDetails')"
|
||||
:tone="billing.longContextBillingApplied ? 'warning' : 'primary'"
|
||||
|
||||
@@ -26,7 +26,7 @@ const outcome = computed(() => usageErrorTitle(props.record))
|
||||
<div class="min-w-0 py-0.5">
|
||||
<div class="flex min-w-0 items-center gap-2">
|
||||
<span
|
||||
class="inline-flex size-5 shrink-0 items-center justify-center rounded-z-sm"
|
||||
class="inline-flex size-5 shrink-0 items-center justify-center"
|
||||
:class="origin.className"
|
||||
role="img"
|
||||
:aria-label="origin.label"
|
||||
|
||||
@@ -79,14 +79,19 @@ const { t } = useI18n({ useScope: 'global' })
|
||||
</template>
|
||||
|
||||
<template #accountPlanType="{ row }">
|
||||
<AccountPlanBadge
|
||||
<div
|
||||
v-if="row.accountUpstreamPeer || row.authenticationKind === 'api_key' || row.accountPlanType"
|
||||
:authentication-kind="row.authenticationKind ?? undefined"
|
||||
:upstream-peer="row.accountUpstreamPeer"
|
||||
:plan-type="row.accountPlanType"
|
||||
:plan-type-display="row.accountPlanTypeDisplay || row.accountPlanType || ''"
|
||||
size="sm"
|
||||
/>
|
||||
class="-mx-2.5 flex min-w-0 justify-center"
|
||||
>
|
||||
<AccountPlanBadge
|
||||
wrap
|
||||
:authentication-kind="row.authenticationKind ?? undefined"
|
||||
:upstream-peer="row.accountUpstreamPeer"
|
||||
:plan-type="row.accountPlanType"
|
||||
:plan-type-display="row.accountPlanTypeDisplay || row.accountPlanType || ''"
|
||||
size="sm"
|
||||
/>
|
||||
</div>
|
||||
<span v-else class="text-z-text-quaternary">—</span>
|
||||
</template>
|
||||
|
||||
|
||||
@@ -24,22 +24,22 @@ export function usageErrorOrigin(record: Pick<UsageErrorFacts, 'errorOrigin'>) {
|
||||
const origins = {
|
||||
proxy: {
|
||||
label: t('usage.error.origin.proxy'),
|
||||
className: 'bg-z-blue-container text-z-blue-on-container',
|
||||
className: 'text-z-blue-text',
|
||||
hint: null,
|
||||
},
|
||||
upstream: {
|
||||
label: t('usage.error.origin.upstream'),
|
||||
className: 'bg-z-error-container text-z-error-on-container',
|
||||
className: 'text-z-error-text',
|
||||
hint: null,
|
||||
},
|
||||
connection: {
|
||||
label: t('usage.error.origin.connection'),
|
||||
className: 'bg-z-warning-container text-z-warning-on-container',
|
||||
className: 'text-z-warning-text',
|
||||
hint: t('usage.error.origin.connectionHint'),
|
||||
},
|
||||
unknown: {
|
||||
label: t('usage.error.origin.unknown'),
|
||||
className: 'bg-z-fill-tertiary text-z-text-secondary',
|
||||
className: 'text-z-text-secondary',
|
||||
hint: t('usage.error.origin.unknownHint'),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@
|
||||
"cacheReadCost": "Cache read cost",
|
||||
"cacheWriteCost": "Cache write cost",
|
||||
"cacheWriteUnitPrice": "Cache write unit price",
|
||||
"fastMode": "Fast mode",
|
||||
"speedMode": "{mode} mode",
|
||||
"longContextBillingDetails": "Long-context cost details",
|
||||
"billingDetails": "Cost details",
|
||||
"viewLongContextBillingDetails": "View long-context cost details",
|
||||
|
||||
@@ -45,7 +45,7 @@
|
||||
"cacheReadCost": "缓存读取费用",
|
||||
"cacheWriteCost": "缓存写入费用",
|
||||
"cacheWriteUnitPrice": "缓存写入单价",
|
||||
"fastMode": "Fast 模式",
|
||||
"speedMode": "{mode} 模式",
|
||||
"longContextBillingDetails": "长上下文计费明细",
|
||||
"billingDetails": "计费明细",
|
||||
"viewLongContextBillingDetails": "查看长上下文计费明细",
|
||||
|
||||
@@ -45,7 +45,7 @@
|
||||
"cacheReadCost": "緩存讀取費用",
|
||||
"cacheWriteCost": "緩存寫入費用",
|
||||
"cacheWriteUnitPrice": "緩存寫入單價",
|
||||
"fastMode": "Fast 模式",
|
||||
"speedMode": "{mode} 模式",
|
||||
"longContextBillingDetails": "長上下文計費明細",
|
||||
"billingDetails": "計費明細",
|
||||
"viewLongContextBillingDetails": "查看長上下文計費明細",
|
||||
|
||||
@@ -327,8 +327,8 @@ const {
|
||||
</template>
|
||||
|
||||
<template #planType="{ row }">
|
||||
<div class="flex min-w-0 justify-center">
|
||||
<AccountPlanBadge :upstream="row.upstream" :authentication-kind="row.authenticationKind" :plan-type="row.planType" :plan-type-display="row.planTypeDisplay" />
|
||||
<div class="-mx-2.5 flex min-w-0 justify-center">
|
||||
<AccountPlanBadge wrap :upstream="row.upstream" :authentication-kind="row.authenticationKind" :plan-type="row.planType" :plan-type-display="row.planTypeDisplay" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ export function useUsageRecordsTable(options: UseUsageRecordsTableOptions) {
|
||||
clientApiKeyName: filterField.value === 'key'
|
||||
? value
|
||||
: undefined,
|
||||
accountName: filterField.value === 'account'
|
||||
accountSearch: filterField.value === 'account'
|
||||
? value
|
||||
: undefined,
|
||||
model: filterField.value === 'model'
|
||||
@@ -74,7 +74,7 @@ export function useUsageRecordsTable(options: UseUsageRecordsTableOptions) {
|
||||
function filterChanged() {
|
||||
const filter = filterParams.value
|
||||
return tableParams.clientApiKeyName !== filter.clientApiKeyName
|
||||
|| tableParams.accountName !== filter.accountName
|
||||
|| tableParams.accountSearch !== filter.accountSearch
|
||||
|| tableParams.model !== filter.model
|
||||
}
|
||||
|
||||
|
||||
+4
-16
@@ -1,21 +1,9 @@
|
||||
# v3.51.0
|
||||
|
||||
## 新增功能
|
||||
|
||||
- 插件可按账号、额度桶和时间范围分页读取已保留的 OpenAI 请求额度观测及区间起点基线,单次查询区间最长 366 天
|
||||
- 调度插件可区分候选账号的会话绑定、优先选择、跟随绑定和固定选号关系
|
||||
|
||||
## 优化改进
|
||||
|
||||
- 账号、Key 和代理的更多操作菜单支持统一的键盘交互
|
||||
- 额度设置在提交时校验并定位错误,预热开关与额度桶标题同行显示
|
||||
- Key 用量页缩窄桌面端模型筛选框
|
||||
# v3.52.2
|
||||
|
||||
## 问题修复
|
||||
|
||||
- OpenAI 严格亲和下,子线程因绑定账号的模型权限受限而等待换号时,超时提示明确说明模型限制
|
||||
- OpenAI Responses WebSocket 按 Codex CLI 0.170.0 的方式分帧发送超过 1 MiB 的文本请求,启用压缩时上游也能完整还原同一条消息
|
||||
|
||||
## 升级须知
|
||||
## 优化改进
|
||||
|
||||
- 调度插件的 `scheduler` 能力升级至 v4,使用 v3 的插件需更新能力声明并适配新增候选字段后使用
|
||||
- 历史额度查询受请求记录保留范围限制,使用观测差值时需处理重置、套餐变化和缺样
|
||||
- 内置 Codex CLI 版本标识更新至 0.170.0,离线时也可使用
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
version: 3.51.0
|
||||
version: 3.52.2
|
||||
published:
|
||||
'3':
|
||||
stable: 3.50.0
|
||||
rc: 3.50.0
|
||||
beta: 3.50.0
|
||||
alpha: 3.50.0
|
||||
stable: 3.52.1
|
||||
rc: 3.52.1
|
||||
beta: 3.52.1
|
||||
alpha: 3.52.1
|
||||
|
||||
Reference in New Issue
Block a user