Skip to content

refactor(ai-adapters): keep provider failures typed through retry logic#560

Draft
cooleryu wants to merge 1 commit intoGCWing:mainfrom
cooleryu:refactor-structured-provider-errors
Draft

refactor(ai-adapters): keep provider failures typed through retry logic#560
cooleryu wants to merge 1 commit intoGCWing:mainfrom
cooleryu:refactor-structured-provider-errors

Conversation

@cooleryu
Copy link
Copy Markdown

Summary

This keeps provider failures structured from the adapter layer through the core retry/error-detail path.

The PR adds a small ProviderError / ProviderErrorKind model in bitfun-ai-adapters, parses provider error payloads from HTTP and streaming responses, and preserves those details as BitFunError::AIProvider instead of flattening them into strings too early.

Motivation

Provider errors currently reach the UI through ErrorCategory / AiErrorDetail, but the adapter boundary can turn them into plain strings first. That makes useful details like provider code, request id, HTTP status, and retryability depend on downstream string matching.

Keeping the error typed at the provider boundary gives retry logic and diagnostics a stable signal, while keeping the existing string-based fallback path for older or unknown adapter errors.

Changes

  • Added ProviderError and ProviderErrorKind in bitfun-ai-adapters.
  • Parses structured provider errors from:
    • non-2xx SSE HTTP responses
    • OpenAI-compatible SSE errors
    • Anthropic-compatible message error events
    • Gemini API errors
    • Responses API errors
  • Added BitFunError::AIProvider and direct mapping to ErrorCategory / AiErrorDetail.
  • Updated stream processing so typed provider errors survive the adapter-to-core boundary.
  • Updated retry decisions so typed retryable provider failures are retried, while quota/auth/billing-style errors are rejected early.
  • Added regression coverage for provider code 1305, HTTP error bodies, structured quota errors, and retry decisions.

Test Plan

  • LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 $HOME/.cargo/bin/cargo test -p bitfun-ai-adapters --lib
  • LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 $HOME/.cargo/bin/cargo test -p bitfun-core structured_provider --lib
  • LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 $HOME/.cargo/bin/cargo test -p bitfun-core agentic::execution::round_executor::tests --lib
  • /Users/behappy/.cargo/bin/cargo test -p bitfun-core --test stream_processor_openai
  • /Users/behappy/.cargo/bin/rustfmt --edition 2021 --check <touched Rust files>
  • git diff --check

Notes

I kept this intentionally bounded to the adapter/core error boundary. The existing AIClient(String) classification stays in place as a fallback, so this should not require a broader error-policy migration or a UI change.

@GCWing GCWing requested a review from wsp1911 April 28, 2026 01:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant