Skip to content

Commit 0a187a4

Browse files
committed
refactor chatgpt turn initialization
1 parent b4fc932 commit 0a187a4

1 file changed

Lines changed: 64 additions & 44 deletions

File tree

src/provider/chatgpt.rs

Lines changed: 64 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -401,17 +401,19 @@ impl ModelSession for OpenAiSubscriptionSession {
401401
let response_request_id = crate::fatal::safe_response_request_id(response.headers());
402402
let mut turn = OpenAiSubscriptionTurn::new_inner(
403403
response.bytes_stream(),
404-
self.config.model.clone(),
405-
response_model,
406-
turn_state
407-
.as_ref()
408-
.and_then(|value| value.to_str().ok())
409-
.map(str::to_owned),
410-
self.binding.clone(),
411-
self.session_id.clone(),
412-
self.context_windows.clone(),
413-
retries.saturating_add(1),
414-
response_request_id,
404+
OpenAiSubscriptionTurnInit {
405+
requested_model: self.config.model.clone(),
406+
header_model: response_model,
407+
turn_state: turn_state
408+
.as_ref()
409+
.and_then(|value| value.to_str().ok())
410+
.map(str::to_owned),
411+
binding: self.binding.clone(),
412+
session_id: self.session_id.clone(),
413+
context_windows: self.context_windows.clone(),
414+
attempt: retries.saturating_add(1),
415+
response_request_id,
416+
},
415417
);
416418
let elapsed = started.elapsed();
417419
if elapsed >= RETRY_BUDGET {
@@ -657,6 +659,17 @@ fn parse_context_windows(value: &Value) -> Result<HashMap<String, u64>, LoopErro
657659

658660
type ByteStream = Pin<Box<dyn Stream<Item = Result<Bytes, reqwest::Error>> + Send>>;
659661

662+
struct OpenAiSubscriptionTurnInit {
663+
requested_model: String,
664+
header_model: Option<String>,
665+
turn_state: Option<String>,
666+
binding: auth::CredentialBinding,
667+
session_id: String,
668+
context_windows: Arc<HashMap<String, u64>>,
669+
attempt: usize,
670+
response_request_id: Option<String>,
671+
}
672+
660673
pub struct OpenAiSubscriptionTurn {
661674
stream: ByteStream,
662675
buffer: Zeroizing<Vec<u8>>,
@@ -720,31 +733,36 @@ impl OpenAiSubscriptionTurn {
720733
) -> Self {
721734
Self::new_inner(
722735
stream,
723-
requested_model,
724-
actual_model,
725-
None,
726-
auth::CredentialBinding {
727-
account_id: "test-account".to_owned(),
728-
generation: "test-generation".to_owned(),
736+
OpenAiSubscriptionTurnInit {
737+
requested_model,
738+
header_model: actual_model,
739+
turn_state: None,
740+
binding: auth::CredentialBinding {
741+
account_id: "test-account".to_owned(),
742+
generation: "test-generation".to_owned(),
743+
},
744+
session_id: "s".to_owned(),
745+
context_windows: Arc::new(HashMap::new()),
746+
attempt: 1,
747+
response_request_id: None,
729748
},
730-
"s".to_owned(),
731-
Arc::new(HashMap::new()),
732-
1,
733-
None,
734749
)
735750
}
736751

737752
fn new_inner(
738753
stream: impl Stream<Item = Result<Bytes, reqwest::Error>> + Send + 'static,
739-
requested_model: String,
740-
header_model: Option<String>,
741-
turn_state: Option<String>,
742-
binding: auth::CredentialBinding,
743-
session_id: String,
744-
context_windows: Arc<HashMap<String, u64>>,
745-
attempt: usize,
746-
response_request_id: Option<String>,
754+
init: OpenAiSubscriptionTurnInit,
747755
) -> Self {
756+
let OpenAiSubscriptionTurnInit {
757+
requested_model,
758+
header_model,
759+
turn_state,
760+
binding,
761+
session_id,
762+
context_windows,
763+
attempt,
764+
response_request_id,
765+
} = init;
748766
Self {
749767
stream: Box::pin(stream),
750768
buffer: Zeroizing::new(Vec::new()),
@@ -2565,11 +2583,11 @@ mod usage_tests {
25652583
use super::{
25662584
CONTINUATION_METADATA, ContinuationContext, GENERATED_IMAGE_METADATA, MAX_RETRIES,
25672585
MAX_RETRY_BACKOFF, OpenAiSubscriptionSession, OpenAiSubscriptionTurn,
2568-
PROVIDER_FINISH_REASONS_METADATA, RETRY_BUDGET, SubscriptionConfig,
2569-
classify_response_failure, classify_top_level_error, map_item, parse_context_windows,
2570-
parse_usage, request_body, retriable_http_status, retriable_status_code,
2571-
retriable_transport_error, retry_backoff, retry_failure, set_provider_finish_reasons,
2572-
tool_output,
2586+
OpenAiSubscriptionTurnInit, PROVIDER_FINISH_REASONS_METADATA, RETRY_BUDGET,
2587+
SubscriptionConfig, classify_response_failure, classify_top_level_error, map_item,
2588+
parse_context_windows, parse_usage, request_body, retriable_http_status,
2589+
retriable_status_code, retriable_transport_error, retry_backoff, retry_failure,
2590+
set_provider_finish_reasons, tool_output,
25732591
};
25742592

25752593
#[test]
@@ -3490,17 +3508,19 @@ data: {"type":"response.output_text.delta","sequence_number":2,"item_id":"item-1
34903508
let request_id = crate::fatal::safe_response_request_id(response.headers());
34913509
OpenAiSubscriptionTurn::new_inner(
34923510
response.bytes_stream(),
3493-
"gpt-5.4".into(),
3494-
None,
3495-
None,
3496-
super::auth::CredentialBinding {
3497-
account_id: "test-account".to_owned(),
3498-
generation: "test-generation".to_owned(),
3511+
OpenAiSubscriptionTurnInit {
3512+
requested_model: "gpt-5.4".into(),
3513+
header_model: None,
3514+
turn_state: None,
3515+
binding: super::auth::CredentialBinding {
3516+
account_id: "test-account".to_owned(),
3517+
generation: "test-generation".to_owned(),
3518+
},
3519+
session_id: "s".to_owned(),
3520+
context_windows: Arc::new(HashMap::new()),
3521+
attempt: 1,
3522+
response_request_id: request_id,
34993523
},
3500-
"s".to_owned(),
3501-
Arc::new(HashMap::new()),
3502-
1,
3503-
request_id,
35043524
)
35053525
}
35063526

0 commit comments

Comments
 (0)