shenyu icon indicating copy to clipboard operation
shenyu copied to clipboard

[Task] Refactor AI Proxy into protocol, provider, and transport layers

Open hengyuss opened this issue 2 months ago • 3 comments

[Task] Refactor AI Proxy into protocol, provider, and transport layers

Scope update

The initial Protocol, Provider, and Transport contracts will be established from a minimal runnable vertical slice instead of attempting to define the complete architecture up front.

The initial contracts are intentionally small and may evolve compatibly as response conversion, additional providers, transport policies, and the final plugin migration are implemented.

Background

The current AI Proxy execution path is tightly coupled to OpenAI Chat Completions and Spring AI types:

  • AiProxyPlugin directly handles OpenAI request/response DTOs and SSE encoding.
  • AiProxyExecutorService mixes upstream invocation, retry, and fallback logic.
  • Adding protocols such as OpenAI Responses or Anthropic Messages requires modifying the main execution flow.
  • Unknown provider-specific fields may be lost during DTO conversion.

This refactor refers to the layered design of Apache APISIX AI Proxy while adapting it to ShenYu's reactive Java architecture.

Target architecture

Client request
      ↓
Protocol: detect, validate, and decode the client protocol
      ↓
Provider: construct the provider-specific upstream request
      ↓
Transport: execute the raw reactive HTTP exchange
      ↓
Provider: handle provider-specific response differences
      ↓
Protocol: encode the client protocol response

AiProxyEngine coordinates these layers. Retry and fallback policies belong to the orchestration layer rather than Transport.

The final AI Proxy data path should not depend on Spring AI clients or DTOs. Other AI plugins may continue using Spring AI.

Delivery plan

  • #7381 establishes the initial contracts with a minimal runnable OpenAI Chat → OpenAI Provider → WebClient Transport vertical slice. It does not migrate AiProxyPlugin.
  • #7382 completes client protocol response handling, streaming semantics, and normalized response/event/error models.
  • #7383 adds provider capability handling and additional OpenAI-compatible and DeepSeek provider behavior.
  • #7384 hardens the WebClient/Reactor Netty transport with production configuration and verification.
  • #7532 completes AiProxyEngine orchestration, including response flow, retry, fallback, and cancellation.
  • #7533 migrates AiProxyPlugin to AiProxyEngine, removes the legacy AI Proxy execution path, and verifies compatibility.

Compatibility requirements

The completed refactor must preserve:

  • existing AiProxyHandle configuration;
  • OpenAI Chat Completions behavior;
  • streaming and non-streaming requests;
  • proxy API key validation;
  • retry and fallback behavior;
  • existing Selector and Rule data;
  • unknown protocol- and provider-specific fields whenever no transformation is required.

Acceptance criteria

  • [ ] Protocol, Provider, and Transport have independent contracts and registries.
  • [ ] Adding a Protocol does not require changing Provider or Transport.
  • [ ] Adding a Provider does not require changing the plugin execution flow.
  • [ ] Streaming responses are forwarded without buffering the complete body.
  • [ ] Upstream status, headers, and error bodies are preserved.
  • [ ] Retry and fallback are coordinated outside Transport.
  • [ ] AiProxyPlugin no longer depends on OpenAI-specific DTOs, OpenAiApi, or other Spring AI clients after the final migration.
  • [ ] Existing OpenAI-compatible behavior remains covered by compatibility tests.
  • [ ] Extension documentation and unit tests are provided.

hengyuss avatar Sep 28 '26 10:09 hengyuss

👋 Hello @yuluo-yx

A new issue has been opened by @hengyuss:

  • Title: [Task] Refactor AI Proxy into protocol, provider, and transport layers
  • Link: https://github.com/apache/shenyu/issues/7351

Please review when you have time. Thank you! 🙏

github-actions[bot] avatar Sep 28 '26 10:09 github-actions[bot]

/assign

hengyuss avatar Sep 28 '26 10:09 hengyuss

Progress update after merged PR #7543:

  • #7381 is complete. #7543 imported the full foundation from #7531; #7531 had no remaining diff and has been closed as superseded.
  • #7532 is complete. Engine response orchestration, retry/fallback, stream-safety, and cancellation landed in #7543.
  • #7533 was already closed by #7543 after the plugin migration.
  • #7382 remains open for the broader normalized Protocol response/event/usage/error model and full focused coverage.
  • #7383 remains open for Provider capability validation and provider-specific mappings/hardening.
  • #7384 remains open for production Transport configuration and verification, including timeouts, proxy/TLS/pool reuse, streaming-safe limits, and backpressure.

Keeping this parent task open until the remaining Protocol, Provider, and Transport hardening work is completed or explicitly descoped.

hengyuss avatar Oct 11 '26 09:10 hengyuss