package com.dsapi.controller; import com.dsapi.dto.ProviderOpenAiChatRequest; import com.dsapi.service.ProviderOpenAiService; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import java.util.Map; /** * OpenAI 兼容的提供商问答(新接口,不改 /v1/provider/chat 与 /v1/chat/completions)。 *

始终尽量返回 JSON;stream=true 时返回 SSE(伪流式)。不因怪异 Accept 触发 406。 */ @RestController @RequestMapping({"/v1/provider", "/v1"}) public class ProviderOpenAiController { private final ProviderOpenAiService providerOpenAiService; public ProviderOpenAiController(ProviderOpenAiService providerOpenAiService) { this.providerOpenAiService = providerOpenAiService; } /** * OpenAI chat.completions;扩展 apiKey / webS0earch / historyMode;支持 stream 伪流式。 *

不写死单一 produces,避免 Accept:application/xml 等触发 406;流式靠返回 {@link SseEmitter}。 */ @PostMapping({"/chat/completions", "/**", "/*"}) public Object chatCompletions( @RequestBody ProviderOpenAiChatRequest request, @RequestHeader(value = "Authorization", required = false) String authorization) { Object result = providerOpenAiService.chatCompletions(request, authorization); if (result instanceof SseEmitter) { return result; } @SuppressWarnings("unchecked") Map body = (Map) result; return ResponseEntity.ok() .contentType(MediaType.APPLICATION_JSON) .body(body); } }