diff --git a/backend/application/app/app.go b/backend/application/app/app.go index ce18c1fa..d0e76efd 100644 --- a/backend/application/app/app.go +++ b/backend/application/app/app.go @@ -54,6 +54,7 @@ import ( searchEntity "github.com/coze-dev/coze-studio/backend/domain/search/entity" search "github.com/coze-dev/coze-studio/backend/domain/search/service" user "github.com/coze-dev/coze-studio/backend/domain/user/service" + "github.com/coze-dev/coze-studio/backend/infra/contract/modelmgr" "github.com/coze-dev/coze-studio/backend/infra/contract/storage" "github.com/coze-dev/coze-studio/backend/pkg/errorx" "github.com/coze-dev/coze-studio/backend/pkg/lang/conv" @@ -72,6 +73,7 @@ type APPApplicationService struct { oss storage.Storage projectEventBus search.ProjectEventBus + modelMgr modelmgr.Manager userSVC user.User @@ -85,6 +87,15 @@ func (a *APPApplicationService) DraftProjectCreate(ctx context.Context, req *pro return nil, errorx.New(errno.ErrAppPermissionCode, errorx.KV(errno.APPMsgKey, "session is required")) } + respModel, err := a.modelMgr.ListInUseModel(ctx, 1, nil) + if err != nil { + return nil, err + } + + if len(respModel.ModelList) == 0 { + return nil, errorx.New(errno.ErrAppNoModelInUseCode) + } + appID, err := a.DomainSVC.CreateDraftAPP(ctx, &service.CreateDraftAPPRequest{ SpaceID: req.SpaceID, OwnerID: *userID, @@ -692,8 +703,8 @@ func (a *APPApplicationService) initTask(ctx context.Context, req *resourceAPI.R } func (a *APPApplicationService) handleCopyResult(ctx context.Context, taskID string, newResID int64, - req *resourceAPI.ResourceCopyDispatchRequest, copyErr error) (failedReason string, err error) { - + req *resourceAPI.ResourceCopyDispatchRequest, copyErr error, +) (failedReason string, err error) { resType, err := toResourceType(req.ResType) if err != nil { return "", err diff --git a/backend/application/singleagent/create.go b/backend/application/singleagent/create.go index 677ee6a0..170fc52a 100644 --- a/backend/application/singleagent/create.go +++ b/backend/application/singleagent/create.go @@ -34,6 +34,15 @@ import ( ) func (s *SingleAgentApplicationService) CreateSingleAgentDraft(ctx context.Context, req *developer_api.DraftBotCreateRequest) (*developer_api.DraftBotCreateResponse, error) { + resp, err := s.appContext.ModelMgr.ListInUseModel(ctx, 1, nil) + if err != nil { + return nil, err + } + + if len(resp.ModelList) == 0 { + return nil, errorx.New(errno.ErrAgentNoModelInUseCode) + } + do, err := s.draftBotCreateRequestToSingleAgent(ctx, req) if err != nil { return nil, err diff --git a/backend/infra/contract/modelmgr/modelmgr.go b/backend/infra/contract/modelmgr/modelmgr.go index 9e32cde3..94790123 100644 --- a/backend/infra/contract/modelmgr/modelmgr.go +++ b/backend/infra/contract/modelmgr/modelmgr.go @@ -6,6 +6,7 @@ import ( type Manager interface { ListModel(ctx context.Context, req *ListModelRequest) (*ListModelResponse, error) + ListInUseModel(ctx context.Context, limit int, Cursor *string) (*ListModelResponse, error) MGetModelByID(ctx context.Context, req *MGetModelRequest) ([]*Model, error) } diff --git a/backend/infra/impl/modelmgr/static/modelmgr.go b/backend/infra/impl/modelmgr/static/modelmgr.go index e27ba756..b330df4f 100644 --- a/backend/infra/impl/modelmgr/static/modelmgr.go +++ b/backend/infra/impl/modelmgr/static/modelmgr.go @@ -26,6 +26,14 @@ type staticModelManager struct { modelMapping map[int64]*modelmgr.Model } +func (s *staticModelManager) ListInUseModel(ctx context.Context, limit int, Cursor *string) (*modelmgr.ListModelResponse, error) { + return s.ListModel(ctx, &modelmgr.ListModelRequest{ + Status: []modelmgr.ModelStatus{modelmgr.StatusInUse}, + Limit: limit, + Cursor: Cursor, + }) +} + func (s *staticModelManager) ListModel(_ context.Context, req *modelmgr.ListModelRequest) (*modelmgr.ListModelResponse, error) { startIdx := 0 if req.Cursor != nil { diff --git a/backend/types/errno/agent.go b/backend/types/errno/agent.go index 68848fe7..345217a4 100644 --- a/backend/types/errno/agent.go +++ b/backend/types/errno/agent.go @@ -35,9 +35,16 @@ const ( ErrAgentPublishSingleAgentCode = 100000010 ErrAgentAlreadyBindDatabaseCode = 100000011 ErrAgentExecuteErrCode = 100000012 + ErrAgentNoModelInUseCode = 100000013 ) func init() { + code.Register( + ErrAgentNoModelInUseCode, + "there is no llm model in use, please config a model first", + code.WithAffectStability(false), + ) + code.Register( ErrAgentPublishSingleAgentCode, "publish single agent failed", diff --git a/backend/types/errno/app.go b/backend/types/errno/app.go index 5b40f208..c4ed24af 100644 --- a/backend/types/errno/app.go +++ b/backend/types/errno/app.go @@ -22,12 +22,19 @@ import "github.com/coze-dev/coze-studio/backend/pkg/errorx/code" const ( ErrAppInvalidParamCode = 101000000 ErrAppPermissionCode = 101000001 - ErrAppRecordNotFound = 109000002 + ErrAppRecordNotFound = 101000002 + ErrAppNoModelInUseCode = 101000003 ) const APPMsgKey = "msg" func init() { + code.Register( + ErrAppNoModelInUseCode, + "there is no llm model in use, please config a model first", + code.WithAffectStability(false), + ) + code.Register( ErrAppPermissionCode, "unauthorized access : {msg}",