|
| 1 | +using Juggle.Application.Models.Response; |
| 2 | +using Juggle.Application.Services.Impl; |
| 3 | +using Microsoft.AspNetCore.Authorization; |
| 4 | +using Microsoft.AspNetCore.Mvc; |
| 5 | + |
| 6 | +namespace Juggle.Api.Controllers.Api; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// AI 大模型控制器:模型配置 + 需求对话生成接口流程编排。 |
| 10 | +/// 支持任意 OpenAI 兼容接口(DeepSeek / 通义千问 / Kimi / OpenAI 等)。 |
| 11 | +/// </summary> |
| 12 | +[ApiController] |
| 13 | +[Route("api/ai")] |
| 14 | +[Authorize] |
| 15 | +public class AiController : ControllerBase |
| 16 | +{ |
| 17 | + private readonly AiService _aiService; |
| 18 | + |
| 19 | + public AiController(AiService aiService) => _aiService = aiService; |
| 20 | + |
| 21 | + /// <summary>读取 AI 模型配置</summary> |
| 22 | + [HttpGet("config")] |
| 23 | + public async Task<ApiResult> GetConfig() |
| 24 | + { |
| 25 | + try |
| 26 | + { |
| 27 | + return ApiResult.Success(await _aiService.GetConfigAsync()); |
| 28 | + } |
| 29 | + catch (Exception ex) { return ApiResult.Fail(ex.Message); } |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary>保存 AI 模型配置(OpenAI 兼容接口地址/密钥/模型名)</summary> |
| 33 | + [HttpPost("config")] |
| 34 | + public async Task<ApiResult> SaveConfig([FromBody] AiConfigRequest req) |
| 35 | + { |
| 36 | + try |
| 37 | + { |
| 38 | + await _aiService.SaveConfigAsync(req.BaseUrl, req.ApiKey, req.Model); |
| 39 | + return ApiResult.Success(); |
| 40 | + } |
| 41 | + catch (Exception ex) { return ApiResult.Fail(ex.Message); } |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary>根据需求对话生成接口流程编排 JSON</summary> |
| 45 | + [HttpPost("generate-flow")] |
| 46 | + public async Task<ApiResult> GenerateFlow([FromBody] AiGenerateFlowRequest req) |
| 47 | + { |
| 48 | + try |
| 49 | + { |
| 50 | + var flow = await _aiService.GenerateFlowAsync(req.Requirement, req.Apis, req.InputParams, req.OutputParams); |
| 51 | + return ApiResult.Success(flow); |
| 52 | + } |
| 53 | + catch (Exception ex) { return ApiResult.Fail(ex.Message); } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +public class AiConfigRequest |
| 58 | +{ |
| 59 | + public string BaseUrl { get; set; } = ""; |
| 60 | + public string ApiKey { get; set; } = ""; |
| 61 | + public string Model { get; set; } = ""; |
| 62 | +} |
| 63 | + |
| 64 | +public class AiGenerateFlowRequest |
| 65 | +{ |
| 66 | + public string Requirement { get; set; } = ""; |
| 67 | + public List<Dictionary<string, object?>>? Apis { get; set; } |
| 68 | + public List<Dictionary<string, object?>>? InputParams { get; set; } |
| 69 | + public List<Dictionary<string, object?>>? OutputParams { get; set; } |
| 70 | +} |
0 commit comments