新增AI服务接口

This commit is contained in:
Liujian
2024-09-25 09:35:58 +08:00
parent c56f6ff1fb
commit be40015526
15 changed files with 375 additions and 204 deletions
+1
View File
@@ -25,5 +25,6 @@ type SimpleProviderItem struct {
type LLMItem struct {
Id string `json:"id"`
Logo string `json:"logo"`
Config string `json:"config"`
Scopes []string `json:"scopes"`
}
+1
View File
@@ -120,6 +120,7 @@ func (i *imlProviderModule) LLMs(ctx context.Context, driver string) ([]*ai_dto.
items = append(items, &ai_dto.LLMItem{
Id: v.Id,
Logo: v.Logo,
Config: p.InvokeConfig().DefaultConfig(),
Scopes: v.Scopes,
})
}
+3
View File
@@ -9,6 +9,8 @@ type CreateService struct {
Logo string `json:"logo"`
Tags []string `json:"tags"`
Catalogue string `json:"catalogue"`
Kind *string `json:"kind,omitempty"`
Provider *string `json:"provider" aocheck:"ai_provider"`
AsApp *bool `json:"as_app"`
AsServer *bool `json:"as_server"`
}
@@ -20,6 +22,7 @@ type EditService struct {
Catalogue *string `json:"catalogue"`
Logo *string `json:"logo"`
Tags *[]string `json:"tags"`
Provider *string `json:"provider" aocheck:"ai_provider"`
}
type CreateApp struct {
+10 -1
View File
@@ -13,6 +13,7 @@ type ServiceItem struct {
Description string `json:"description"`
CreateTime auto.TimeLabel `json:"create_time"`
UpdateTime auto.TimeLabel `json:"update_time"`
Provider *auto.Label `json:"provider,omitempty" aolabel:"ai_provider"`
CanDelete bool `json:"can_delete"`
}
@@ -54,6 +55,7 @@ type Service struct {
Catalogue auto.Label `json:"catalogue" aolabel:"catalogue"`
Tags []auto.Label `json:"tags" aolabel:"tag"`
Logo string `json:"logo"`
Provider *auto.Label `json:"provider,omitempty" aolabel:"ai_provider"`
AsServer bool `json:"as_server"`
AsApp bool `json:"as_app"`
}
@@ -69,7 +71,8 @@ type App struct {
}
func ToService(model *service.Service) *Service {
return &Service{
s := &Service{
Id: model.Id,
Name: model.Name,
Prefix: model.Prefix,
@@ -83,6 +86,12 @@ func ToService(model *service.Service) *Service {
AsServer: model.AsServer,
AsApp: model.AsApp,
}
switch model.Kind {
case service.AIService:
provider := auto.UUID(model.AdditionalConfig["provider"])
s.Provider = &provider
}
return s
}
type MemberItem struct {
+160 -115
View File
@@ -48,11 +48,11 @@ type imlServiceModule struct {
teamService team.ITeamService `autowired:""`
teamMemberService team_member.ITeamMemberService `autowired:""`
tagService tag.ITagService `autowired:""`
serviceDocService service_doc.IDocService `autowired:""`
serviceTagService service_tag.ITagService `autowired:""`
apiService api.IAPIService `autowired:""`
apiDocService api_doc.IAPIDocService `autowired:""`
transaction store.ITransaction `autowired:""`
serviceTagService service_tag.ITagService `autowired:""`
apiService api.IAPIService `autowired:""`
apiDocService api_doc.IAPIDocService `autowired:""`
transaction store.ITransaction `autowired:""`
}
func (i *imlServiceModule) ExportAll(ctx context.Context) ([]*service_dto.ExportService, error) {
@@ -82,10 +82,10 @@ func (i *imlServiceModule) ExportAll(ctx context.Context) ([]*service_dto.Export
serviceTagMap[st.Sid] = append(serviceTagMap[st.Sid], tagMap[st.Tid].Name)
}
docMap, err := i.serviceDocService.Map(ctx, serviceIds...)
if err != nil {
return nil, err
}
//docMap, err := i.serviceDocService.Map(ctx, serviceIds...)
//if err != nil {
// return nil, err
//}
items := make([]*service_dto.ExportService, 0, len(services))
for _, s := range services {
@@ -99,9 +99,9 @@ func (i *imlServiceModule) ExportAll(ctx context.Context) ([]*service_dto.Export
Catalogue: s.Catalogue,
Logo: s.Logo,
}
if v, ok := docMap[s.Id]; ok {
info.Doc = v.Doc
}
//if v, ok := docMap[s.Id]; ok {
// info.Doc = v.Doc
//}
if tags, ok := serviceTagMap[s.Id]; ok {
info.Tags = tags
}
@@ -111,11 +111,12 @@ func (i *imlServiceModule) ExportAll(ctx context.Context) ([]*service_dto.Export
}
func (i *imlServiceModule) searchMyServices(ctx context.Context, teamId string, keyword string) ([]*service.Service, error) {
func (i *imlServiceModule) searchMyServices(ctx context.Context, teamId string, keyword string, kind service.Kind) ([]*service.Service, error) {
userID := utils.UserId(ctx)
condition := make(map[string]interface{})
condition["as_server"] = true
condition["kind"] = kind.Int()
if teamId != "" {
_, err := i.teamService.Get(ctx, teamId)
if err != nil {
@@ -135,8 +136,8 @@ func (i *imlServiceModule) searchMyServices(ctx context.Context, teamId string,
}
func (i *imlServiceModule) SearchMyServices(ctx context.Context, teamId string, keyword string) ([]*service_dto.ServiceItem, error) {
services, err := i.searchMyServices(ctx, teamId, keyword)
func (i *imlServiceModule) SearchMyServicesByKind(ctx context.Context, teamId string, keyword string, kind string) ([]*service_dto.ServiceItem, error) {
services, err := i.searchMyServices(ctx, teamId, keyword, service.Kind(kind))
if err != nil {
return nil, err
}
@@ -154,79 +155,74 @@ func (i *imlServiceModule) SearchMyServices(ctx context.Context, teamId string,
continue
}
apiCount := apiCountMap[model.Id]
items = append(items, &service_dto.ServiceItem{
Id: model.Id,
Name: model.Name,
Description: model.Description,
CreateTime: auto.TimeLabel(model.CreateTime),
UpdateTime: auto.TimeLabel(model.UpdateTime),
Team: auto.UUID(model.Team),
ApiNum: apiCount,
CanDelete: apiCount == 0,
})
item := toServiceItem(service.Kind(kind), model)
item.ApiNum = apiCount
item.CanDelete = apiCount == 0
items = append(items, item)
}
return items, nil
}
func (i *imlServiceModule) SimpleAPPS(ctx context.Context, keyword string) ([]*service_dto.SimpleServiceItem, error) {
w := make(map[string]interface{})
w["as_app"] = true
services, err := i.serviceService.Search(ctx, keyword, w)
if err != nil {
return nil, err
}
return utils.SliceToSlice(services, func(p *service.Service) *service_dto.SimpleServiceItem {
return &service_dto.SimpleServiceItem{
Id: p.Id,
Name: p.Name,
Description: p.Description,
//func (i *imlServiceModule) SimpleAPPS(ctx context.Context, keyword string) ([]*service_dto.SimpleServiceItem, error) {
// w := make(map[string]interface{})
// w["as_app"] = true
// services, err := i.serviceService.Search(ctx, keyword, w)
// if err != nil {
// return nil, err
// }
// return utils.SliceToSlice(services, func(p *service.Service) *service_dto.SimpleServiceItem {
// return &service_dto.SimpleServiceItem{
// Id: p.Id,
// Name: p.Name,
// Description: p.Description,
//
// Team: auto.UUID(p.Team),
// }
// }), nil
//}
Team: auto.UUID(p.Team),
}
}), nil
}
func (i *imlServiceModule) Simple(ctx context.Context, keyword string) ([]*service_dto.SimpleServiceItem, error) {
w := make(map[string]interface{})
w["as_server"] = true
services, err := i.serviceService.Search(ctx, keyword, w)
if err != nil {
return nil, err
}
items := make([]*service_dto.SimpleServiceItem, 0, len(services))
for _, p := range services {
items = append(items, &service_dto.SimpleServiceItem{
Id: p.Id,
Name: p.Name,
Description: p.Description,
Team: auto.UUID(p.Team),
})
}
return items, nil
}
func (i *imlServiceModule) MySimple(ctx context.Context, keyword string) ([]*service_dto.SimpleServiceItem, error) {
services, err := i.searchMyServices(ctx, "", keyword)
if err != nil {
return nil, err
}
items := make([]*service_dto.SimpleServiceItem, 0, len(services))
for _, p := range services {
items = append(items, &service_dto.SimpleServiceItem{
Id: p.Id,
Name: p.Name,
Description: p.Description,
Team: auto.UUID(p.Team),
})
}
return items, nil
}
//func (i *imlServiceModule) Simple(ctx context.Context, keyword string) ([]*service_dto.SimpleServiceItem, error) {
// w := make(map[string]interface{})
// w["as_server"] = true
//
// services, err := i.serviceService.Search(ctx, keyword, w)
// if err != nil {
// return nil, err
// }
//
// items := make([]*service_dto.SimpleServiceItem, 0, len(services))
// for _, p := range services {
//
// items = append(items, &service_dto.SimpleServiceItem{
// Id: p.Id,
// Name: p.Name,
// Description: p.Description,
// Team: auto.UUID(p.Team),
// })
// }
// return items, nil
//}
//
//func (i *imlServiceModule) MySimple(ctx context.Context, keyword string) ([]*service_dto.SimpleServiceItem, error) {
// services, err := i.searchMyServices(ctx, "", keyword)
//
// if err != nil {
// return nil, err
// }
//
// items := make([]*service_dto.SimpleServiceItem, 0, len(services))
// for _, p := range services {
//
// items = append(items, &service_dto.SimpleServiceItem{
// Id: p.Id,
// Name: p.Name,
// Description: p.Description,
// Team: auto.UUID(p.Team),
// })
// }
// return items, nil
//}
func (i *imlServiceModule) Get(ctx context.Context, id string) (*service_dto.Service, error) {
serviceInfo, err := i.serviceService.Get(ctx, id)
@@ -245,7 +241,7 @@ func (i *imlServiceModule) Get(ctx context.Context, id string) (*service_dto.Ser
return s, nil
}
func (i *imlServiceModule) Search(ctx context.Context, teamID string, keyword string) ([]*service_dto.ServiceItem, error) {
func (i *imlServiceModule) Search(ctx context.Context, teamID string, keyword string, kind string) ([]*service_dto.ServiceItem, error) {
var list []*service.Service
var err error
if teamID != "" {
@@ -253,9 +249,9 @@ func (i *imlServiceModule) Search(ctx context.Context, teamID string, keyword st
if err != nil {
return nil, err
}
list, err = i.serviceService.Search(ctx, keyword, map[string]interface{}{"team": teamID, "as_server": true}, "update_at desc")
list, err = i.serviceService.Search(ctx, keyword, map[string]interface{}{"team": teamID, "as_server": true, "kind": service.Kind(kind).Int()}, "update_at desc")
} else {
list, err = i.serviceService.Search(ctx, keyword, map[string]interface{}{"as_server": true}, "update_at desc")
list, err = i.serviceService.Search(ctx, keyword, map[string]interface{}{"as_server": true, "kind": service.Kind(kind).Int()}, "update_at desc")
}
if err != nil {
return nil, err
@@ -273,38 +269,64 @@ func (i *imlServiceModule) Search(ctx context.Context, teamID string, keyword st
items := make([]*service_dto.ServiceItem, 0, len(list))
for _, model := range list {
apiCount := apiCountMap[model.Id]
items = append(items, &service_dto.ServiceItem{
Id: model.Id,
Name: model.Name,
Description: model.Description,
CreateTime: auto.TimeLabel(model.CreateTime),
UpdateTime: auto.TimeLabel(model.UpdateTime),
Team: auto.UUID(model.Team),
ApiNum: apiCount,
CanDelete: apiCount == 0,
})
item := toServiceItem(service.Kind(kind), model)
item.ApiNum = apiCount
item.CanDelete = apiCount == 0
items = append(items, item)
}
return items, nil
}
func toServiceItem(kind service.Kind, model *service.Service) *service_dto.ServiceItem {
item := &service_dto.ServiceItem{
Id: model.Id,
Name: model.Name,
Description: model.Description,
CreateTime: auto.TimeLabel(model.CreateTime),
UpdateTime: auto.TimeLabel(model.UpdateTime),
Team: auto.UUID(model.Team),
}
switch kind {
case service.RestService:
return item
case service.AIService:
provider := auto.UUID(model.AdditionalConfig["provider"])
item.Provider = &provider
return item
default:
return item
}
}
func (i *imlServiceModule) Create(ctx context.Context, teamID string, input *service_dto.CreateService) (*service_dto.Service, error) {
if input.Id == "" {
input.Id = uuid.New().String()
}
mo := &service.Create{
Id: input.Id,
Name: input.Name,
Description: input.Description,
Team: teamID,
ServiceType: service.ServiceType(input.ServiceType),
Catalogue: input.Catalogue,
Prefix: input.Prefix,
Logo: input.Logo,
Id: input.Id,
Name: input.Name,
Description: input.Description,
Team: teamID,
ServiceType: service.ServiceType(input.ServiceType),
Catalogue: input.Catalogue,
Prefix: input.Prefix,
Logo: input.Logo,
AdditionalConfig: make(map[string]string),
}
if mo.ServiceType == service.PublicService && mo.Catalogue == "" {
return nil, fmt.Errorf("catalogue can not be empty")
}
if input.Kind != nil {
mo.Kind = service.Kind(*input.Kind)
switch mo.Kind {
case service.AIService:
if input.Provider == nil {
return nil, fmt.Errorf("ai service: provider can not be empty")
}
mo.AdditionalConfig["provider"] = *input.Provider
}
}
if input.AsApp == nil {
// 默认值为false
mo.AsApp = false
@@ -343,10 +365,18 @@ func (i *imlServiceModule) Create(ctx context.Context, teamID string, input *ser
}
func (i *imlServiceModule) Edit(ctx context.Context, id string, input *service_dto.EditService) (*service_dto.Service, error) {
_, err := i.serviceService.Get(ctx, id)
info, err := i.serviceService.Get(ctx, id)
if err != nil {
return nil, err
}
switch info.Kind {
case service.AIService:
if input.Provider != nil {
info.AdditionalConfig["provider"] = *input.Provider
}
}
err = i.transaction.Transaction(ctx, func(ctx context.Context) error {
serviceType := (*service.ServiceType)(input.ServiceType)
if serviceType != nil && *serviceType == service.PublicService {
@@ -356,11 +386,12 @@ func (i *imlServiceModule) Edit(ctx context.Context, id string, input *service_d
}
err = i.serviceService.Save(ctx, id, &service.Edit{
Name: input.Name,
Description: input.Description,
Logo: input.Logo,
ServiceType: serviceType,
Catalogue: input.Catalogue,
Name: input.Name,
Description: input.Description,
Logo: input.Logo,
ServiceType: serviceType,
Catalogue: input.Catalogue,
AdditionalConfig: &info.AdditionalConfig,
})
if err != nil {
return err
@@ -390,9 +421,18 @@ func (i *imlServiceModule) Edit(ctx context.Context, id string, input *service_d
return i.Get(ctx, id)
}
func (i *imlServiceModule) Delete(ctx context.Context, id string) error {
err := i.transaction.Transaction(ctx, func(ctx context.Context) error {
func (i *imlServiceModule) Delete(ctx context.Context, id string, kind string) error {
info, err := i.serviceService.Get(ctx, id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil
}
return err
}
if info.Kind.Int() != service.Kind(kind).Int() {
return fmt.Errorf("kind is not match")
}
err = i.transaction.Transaction(ctx, func(ctx context.Context) error {
count, err := i.apiService.CountByService(ctx, id)
if err != nil {
return err
@@ -441,7 +481,12 @@ func (i *imlServiceModule) getTagUuids(ctx context.Context, tags []string) ([]st
return tagList, nil
}
func (i *imlServiceModule) ServiceDoc(ctx context.Context, pid string) (*serviceDto.ServiceDoc, error) {
type imlServiceDocModule struct {
serviceService service.IServiceService `autowired:""`
serviceDocService service_doc.IDocService `autowired:""`
}
func (i *imlServiceDocModule) ServiceDoc(ctx context.Context, pid string) (*serviceDto.ServiceDoc, error) {
_, err := i.serviceService.Check(ctx, pid, map[string]bool{"as_server": true})
if err != nil {
@@ -473,7 +518,7 @@ func (i *imlServiceModule) ServiceDoc(ctx context.Context, pid string) (*service
}, nil
}
func (i *imlServiceModule) SaveServiceDoc(ctx context.Context, pid string, input *serviceDto.SaveServiceDoc) error {
func (i *imlServiceDocModule) SaveServiceDoc(ctx context.Context, pid string, input *serviceDto.SaveServiceDoc) error {
_, err := i.serviceService.Check(ctx, pid, map[string]bool{"as_server": true})
if err != nil {
+13 -6
View File
@@ -14,21 +14,23 @@ type IServiceModule interface {
// Get 获取项目信息
Get(ctx context.Context, id string) (*service_dto.Service, error)
// Search 搜索项目
Search(ctx context.Context, teamID string, keyword string) ([]*service_dto.ServiceItem, error)
// SearchMyServices 搜索
SearchMyServices(ctx context.Context, teamId string, keyword string) ([]*service_dto.ServiceItem, error)
Search(ctx context.Context, teamID string, keyword string, kind string) ([]*service_dto.ServiceItem, error)
// SearchMyServicesByKind 搜索
SearchMyServicesByKind(ctx context.Context, teamId string, keyword string, kind string) ([]*service_dto.ServiceItem, error)
// Create 创建
Create(ctx context.Context, teamID string, input *service_dto.CreateService) (*service_dto.Service, error)
// Edit 编辑
Edit(ctx context.Context, id string, input *service_dto.EditService) (*service_dto.Service, error)
// Delete 删除项目
Delete(ctx context.Context, id string) error
Delete(ctx context.Context, id string, kind string) error
// Simple 获取简易项目列表
Simple(ctx context.Context, keyword string) ([]*service_dto.SimpleServiceItem, error)
//Simple(ctx context.Context, keyword string) ([]*service_dto.SimpleServiceItem, error)
// MySimple 获取我的简易项目列表
MySimple(ctx context.Context, keyword string) ([]*service_dto.SimpleServiceItem, error)
//MySimple(ctx context.Context, keyword string) ([]*service_dto.SimpleServiceItem, error)
}
type IServiceDocModule interface {
ServiceDoc(ctx context.Context, pid string) (*service_dto.ServiceDoc, error)
// SaveServiceDoc 保存服务文档
SaveServiceDoc(ctx context.Context, pid string, input *service_dto.SaveServiceDoc) error
@@ -73,4 +75,9 @@ func init() {
return reflect.ValueOf(appModule)
})
serviceDocModule := new(imlServiceDocModule)
autowire.Auto[IServiceDocModule](func() reflect.Value {
return reflect.ValueOf(serviceDocModule)
})
}