From 710f0daff34e4ae02cf906841117449bcf114198 Mon Sep 17 00:00:00 2001 From: dev Date: Wed, 21 Oct 2026 22:02:22 +0000 Subject: Add idempotency support to aichat HTTP clients - Add boolean to client config. When enabled, a fresh (UUID) header is sent with each logical request attempt so the outer retry loop reuses the same submission instead of starting duplicate work on providers that support it (e.g. Sail Research). The key is generated once per logical request (before the retry loop) and reused across all retry attempts via the cloned ChatCompletionsData/request builders, so providers debounce duplicate generations. - Wire the header in request_builder (macro-based clients) and Bedrock's chat_completions_builder. - Document the option in config.example.yaml. --- src/client/bedrock.rs | 3 +++ src/client/common.rs | 11 +++++++++++ 2 files changed, 14 insertions(+) (limited to 'src') diff --git a/src/client/bedrock.rs b/src/client/bedrock.rs index 88d57d9..3f99713 100644 --- a/src/client/bedrock.rs +++ b/src/client/bedrock.rs @@ -61,6 +61,9 @@ impl BedrockClient { let mut request_data = RequestData::new("", body); self.patch_request_data(&mut request_data); + if self.extra_config().is_some_and(|v| v.idempotency()) { + request_data.header("idempotency-key", uuid::Uuid::new_v4().to_string()); + } let RequestData { url: _, headers, diff --git a/src/client/common.rs b/src/client/common.rs index a561ed4..d0c23e9 100644 --- a/src/client/common.rs +++ b/src/client/common.rs @@ -224,6 +224,9 @@ pub trait Client: Sync + Send { mut request_data: RequestData, ) -> RequestBuilder { self.patch_request_data(&mut request_data); + if self.extra_config().is_some_and(|v| v.idempotency()) { + request_data.header("idempotency-key", uuid::Uuid::new_v4().to_string()); + } request_data.into_builder(client) } @@ -273,6 +276,7 @@ pub struct ExtraConfig { pub connect_timeout: Option, pub timeout: Option, pub retries: Option, + pub idempotency: Option, pub retry_delay_ms: Option, pub retry_max_delay_ms: Option, } @@ -295,6 +299,13 @@ impl ExtraConfig { self.retries.unwrap_or(0) } + /// Whether to send an `Idempotency-Key` header so retries reuse the same + /// submission instead of starting duplicate work (supported by providers + /// such as Sail Research). Defaults to false. + pub fn idempotency(&self) -> bool { + self.idempotency.unwrap_or(false) + } + /// Base delay between retries in milliseconds. Defaults to 1000. pub fn retry_delay(&self) -> Duration { Duration::from_millis(self.retry_delay_ms.unwrap_or(1000)) -- cgit v1.2.3