| 28 | } |
| 29 | |
| 30 | func (i *imlAPIController) Create(ctx *gin.Context, serviceId string, input *ai_api_dto.CreateAPI) (*ai_api_dto.API, error) { |
| 31 | _, err := i.serviceModule.Get(ctx, serviceId) |
| 32 | if err != nil { |
| 33 | return nil, err |
| 34 | } |
| 35 | if input.Id == "" { |
| 36 | input.Id = uuid.New().String() |
| 37 | } |
| 38 | err = i.transaction.Transaction(ctx, func(txCtx context.Context) error { |
| 39 | err = i.module.Create(ctx, serviceId, input) |
| 40 | if err != nil { |
| 41 | return err |
| 42 | } |
| 43 | plugins := make(map[string]api.PluginSetting) |
| 44 | if input.AiPrompt != nil { |
| 45 | plugins["ai_prompt"] = api.PluginSetting{ |
| 46 | Config: plugin_model.ConfigType{ |
| 47 | "prompt": input.AiPrompt.Prompt, |
| 48 | "variables": input.AiPrompt.Variables, |
| 49 | }, |
| 50 | } |
| 51 | } |
| 52 | if input.AiModel != nil { |
| 53 | provider := ai_provider_local.ProviderLocal |
| 54 | if input.AiModel.Type != "local" { |
| 55 | provider = input.AiModel.Provider |
| 56 | } |
| 57 | plugins["ai_formatter"] = api.PluginSetting{ |
| 58 | Config: plugin_model.ConfigType{ |
| 59 | "model": input.AiModel.Name, |
| 60 | "provider": provider, |
| 61 | "config": input.AiModel.Config, |
| 62 | }, |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | _, err = i.routerModule.Create(ctx, serviceId, &router_dto.Create{ |
| 67 | Id: input.Id, |
| 68 | Name: input.Name, |
| 69 | Path: input.Path, |
| 70 | Methods: []string{ |
| 71 | http.MethodPost, |
| 72 | }, |
| 73 | Description: input.Description, |
| 74 | Protocols: []string{"http", "https"}, |
| 75 | MatchRules: nil, |
| 76 | Proxy: &router_dto.InputProxy{ |
| 77 | Path: input.Path, |
| 78 | Timeout: input.Timeout, |
| 79 | Retry: input.Retry, |
| 80 | Plugins: plugins, |
| 81 | }, |
| 82 | //Upstream: input.AiModel.Provider, |
| 83 | Disable: false, |
| 84 | }) |
| 85 | |
| 86 | return err |
| 87 | }) |