diff --git a/agent/app/dto/agents.go b/agent/app/dto/agents.go index e6d21fbe1..f78bae9b6 100644 --- a/agent/app/dto/agents.go +++ b/agent/app/dto/agents.go @@ -420,7 +420,7 @@ type AgentAccountInfo struct { VerifyModel string `json:"verifyModel"` VerifiedAt *time.Time `json:"verifiedAt"` VerifyError string `json:"verifyError"` - Verified bool `json:"verified"` + Status string `json:"status"` ValidateAvailability bool `json:"validateAvailability"` Remark string `json:"remark"` CreatedAt time.Time `json:"createdAt"` diff --git a/agent/app/model/agent_account.go b/agent/app/model/agent_account.go index 15976dc6c..8a44b2729 100644 --- a/agent/app/model/agent_account.go +++ b/agent/app/model/agent_account.go @@ -16,7 +16,7 @@ type AgentAccount struct { RememberAPIKey bool `json:"rememberApiKey"` VerifiedAt *time.Time `json:"verifiedAt"` VerifyError string `json:"verifyError"` - Verified bool `json:"verified"` + Status string `json:"status"` ValidateAvailability bool `json:"validateAvailability"` Remark string `json:"remark"` MasterAccountID uint `json:"masterAccountId" gorm:"index"` diff --git a/agent/app/provider/verify.go b/agent/app/provider/verify.go index 30ab2d956..35f3128e4 100644 --- a/agent/app/provider/verify.go +++ b/agent/app/provider/verify.go @@ -42,7 +42,7 @@ func VerifyAccount(provider, apiType, authMode, baseURL, apiKey, model string) e httpReq.Header.Set(key, value) } httpReq.Header.Set("Accept", "application/json") - resp, err := aihttp.NewClient(defaultVerifyTimeout).Do(httpReq) + resp, err := aihttp.NewClient(15 * time.Second).Do(httpReq) if err != nil { return buserr.WithErr("ErrAgentAccountUnavailable", aihttp.SafeError(err, req.URL, apiKey)) } diff --git a/agent/app/repo/agent_account.go b/agent/app/repo/agent_account.go index 5b1950efa..0e9facf97 100644 --- a/agent/app/repo/agent_account.go +++ b/agent/app/repo/agent_account.go @@ -2,8 +2,10 @@ package repo import ( "strings" + "time" "github.com/1Panel-dev/1Panel/agent/app/model" + "github.com/1Panel-dev/1Panel/agent/constant" "github.com/1Panel-dev/1Panel/agent/global" "gorm.io/gorm" ) @@ -17,6 +19,8 @@ type IAgentAccountRepo interface { Create(account *model.AgentAccount) error Save(account *model.AgentAccount) error UpdateVerification(account *model.AgentAccount) error + UpdateStatus(id uint, status, verifyError string) error + InterruptVerification() error ResetVerificationByAppInstallID(appInstallID uint) error DeleteByID(id uint) error List(opts ...DBOption) ([]model.AgentAccount, error) @@ -62,7 +66,7 @@ func (a AgentAccountRepo) Save(account *model.AgentAccount) error { func (a AgentAccountRepo) UpdateVerification(account *model.AgentAccount) error { result := getDb().Model(&model.AgentAccount{}).Where("id = ?", account.ID).Updates(map[string]interface{}{ - "verified": account.Verified, + "status": account.Status, "verified_at": account.VerifiedAt, "verify_error": account.VerifyError, "verify_model": account.VerifyModel, @@ -76,9 +80,21 @@ func (a AgentAccountRepo) UpdateVerification(account *model.AgentAccount) error return nil } +func (a AgentAccountRepo) UpdateStatus(id uint, status, verifyError string) error { + return getDb().Model(&model.AgentAccount{}).Where("id = ?", id).UpdateColumns(map[string]interface{}{"status": status, "verify_error": verifyError}).Error +} + +func (a AgentAccountRepo) InterruptVerification() error { + return getDb().Model(&model.AgentAccount{}).Where("status = ?", constant.StatusWaiting).Updates(map[string]interface{}{ + "status": constant.StatusFailed, + "verified_at": time.Now(), + "verify_error": constant.InterruptedMsg, + }).Error +} + func (a AgentAccountRepo) ResetVerificationByAppInstallID(appInstallID uint) error { return getDb().Model(&model.AgentAccount{}).Where("app_install_id = ? AND validate_availability = ?", appInstallID, true).Updates(map[string]interface{}{ - "verified": false, + "status": constant.StatusPending, "verified_at": nil, "verify_error": "", }).Error diff --git a/agent/app/service/agents.go b/agent/app/service/agents.go index e2070a325..2b1ff12fd 100644 --- a/agent/app/service/agents.go +++ b/agent/app/service/agents.go @@ -198,7 +198,7 @@ func (a AgentService) Create(req dto.AgentCreateReq) (*dto.AgentItem, error) { if err != nil { return nil, err } - if account.ValidateAvailability && !account.Verified { + if account.ValidateAvailability && account.Status != constant.StatusSuccess { return nil, buserr.New("ErrAgentAccountNotVerified") } provider = account.Provider @@ -721,7 +721,7 @@ func (a AgentService) ensureBatchInstallAccount(req dto.AgentBatchInstallReq) (u account.APIType = snapshot.APIType account.AuthMode = snapshot.AuthMode account.Remark = snapshot.Remark - account.Verified = true + account.Status = constant.StatusSuccess initialModels, err := buildInitialAgentAccountModels(account, req.AccountModels) if err != nil { @@ -1070,11 +1070,12 @@ func (a AgentService) CreateAccount(req dto.AgentAccountCreateReq) error { APIType: resolvedInput.APIType, AuthMode: resolvedInput.AuthMode, VerifyModel: verifyModel, - Verified: validateAvailability, + Status: constant.StatusPending, ValidateAvailability: validateAvailability, Remark: req.Remark, } if validateAvailability { + account.Status = constant.StatusSuccess now := time.Now() account.VerifiedAt = &now } @@ -1181,13 +1182,13 @@ func (a AgentService) UpdateAccount(req dto.AgentAccountUpdateReq) error { } } if account.ValidateAvailability && (!validateAvailability || connectionChanged) { - account.Verified = false + account.Status = constant.StatusPending account.VerifiedAt = nil account.VerifyError = "" } if shouldVerify { now := time.Now() - account.Verified = true + account.Status = constant.StatusSuccess account.VerifiedAt = &now account.VerifyError = "" } @@ -1282,7 +1283,7 @@ func (a AgentService) PageAccounts(req dto.AgentAccountSearch) (int64, []dto.Age APIType: item.APIType, AuthMode: item.AuthMode, VerifyModel: item.VerifyModel, - Verified: item.Verified, + Status: item.Status, ValidateAvailability: item.ValidateAvailability, VerifiedAt: item.VerifiedAt, VerifyError: item.VerifyError, @@ -1357,7 +1358,7 @@ func (a AgentService) UpdateAccountVerifyModel(req dto.AgentAccountVerifyModelUp previousVerifyModel = models[0].ID } if account.ValidateAvailability && !sameProviderModelID(account.Provider, previousVerifyModel, record.Model) { - account.Verified = false + account.Status = constant.StatusPending account.VerifiedAt = nil account.VerifyError = "" } @@ -1496,7 +1497,7 @@ func (a AgentService) UpdateAccountModel(req dto.AgentAccountModelUpdateReq) err return err } if account.ValidateAvailability && !sameProviderModelID(account.Provider, account.VerifyModel, verifyModel) { - account.Verified, account.VerifiedAt, account.VerifyError = false, nil, "" + account.Status, account.VerifiedAt, account.VerifyError = constant.StatusPending, nil, "" } account.VerifyModel = verifyModel rows := make([]model.AgentAccountModel, 0, len(models)) @@ -1612,10 +1613,15 @@ func (a AgentService) VerifyAccount(req dto.AgentAccountVerifyReq) error { _, err := resolveAgentAccountInput(req.Provider, req.APIType, req.AuthMode, req.APIKey, req.BaseURL, req.Model, true, false) return err } + agentAccountMutationMu.Lock() + defer agentAccountMutationMu.Unlock() account, err := agentAccountRepo.GetFirst(repo.WithByID(req.ID)) if err != nil { return err } + if account.Status == constant.StatusWaiting { + return buserr.WithDetail("ErrInvalidParams", "model service verification is already in progress", nil) + } if !account.ValidateAvailability { return nil } @@ -1623,21 +1629,27 @@ func (a AgentService) VerifyAccount(req dto.AgentAccountVerifyReq) error { if err != nil { return err } + if err := agentAccountRepo.UpdateStatus(account.ID, constant.StatusWaiting, ""); err != nil { + return err + } + account.Status = constant.StatusWaiting + account.VerifyError = "" + agentAccountMutationMu.Unlock() verifyModel, verifyErr := resolveAgentAccountVerifyModel(account.Provider, account.VerifyModel, models) if verifyErr == nil { verifyErr = providercatalog.VerifyAccount(account.Provider, account.APIType, account.AuthMode, account.BaseURL, account.APIKey, verifyModel) } agentAccountMutationMu.Lock() - defer agentAccountMutationMu.Unlock() if err := ensureAgentAccountUnchanged(account, models); err != nil { - return err + return errors.Join(err, agentAccountRepo.UpdateStatus(account.ID, constant.StatusFailed, err.Error())) } now := time.Now() account.VerifyModel = verifyModel account.VerifiedAt = &now - account.Verified = verifyErr == nil + account.Status = constant.StatusSuccess account.VerifyError = "" if verifyErr != nil { + account.Status = constant.StatusFailed account.VerifyError = verifyErr.Error() } err = agentAccountRepo.UpdateVerification(account) diff --git a/agent/app/service/agents_local.go b/agent/app/service/agents_local.go index 2ed2a5cc9..6ac9f657b 100644 --- a/agent/app/service/agents_local.go +++ b/agent/app/service/agents_local.go @@ -58,7 +58,7 @@ func SyncLocalModelAccount(install model.AppInstall, accountID uint, baseURL str if install.App.Key == "ollama" { apiType, apiKey = "openai-responses", "ollama-local" } - account = &model.AgentAccount{Name: name, Provider: install.App.Key, BaseURL: baseURL, APIType: apiType, APIKey: apiKey, RememberAPIKey: true, ValidateAvailability: true} + account = &model.AgentAccount{Name: name, Provider: install.App.Key, BaseURL: baseURL, APIType: apiType, APIKey: apiKey, RememberAPIKey: true, ValidateAvailability: true, Status: constant.StatusPending} } accountChanged := account.ID == 0 || account.AppInstallID != install.ID || (updateBaseURL && account.BaseURL != baseURL) if apiKey != nil && account.APIKey != strings.TrimSpace(*apiKey) { @@ -82,7 +82,7 @@ func SyncLocalModelAccount(install model.AppInstall, accountID uint, baseURL str accountChanged = true } if accountChanged && account.ValidateAvailability { - account.Verified = false + account.Status = constant.StatusPending account.VerifiedAt = nil account.VerifyError = "" } @@ -129,7 +129,7 @@ func UpdateLocalModelAccountAddress(accountID uint, previousURL, baseURL string) } account.BaseURL = baseURL if account.ValidateAvailability { - account.Verified = false + account.Status = constant.StatusPending account.VerifiedAt = nil account.VerifyError = "" } @@ -211,7 +211,7 @@ func updateOllamaModelPool(modelName string, remove bool) error { if sameProviderModelID("ollama", account.VerifyModel, modelName) { account.VerifyModel = "" if account.ValidateAvailability { - account.Verified = false + account.Status = constant.StatusPending account.VerifiedAt = nil account.VerifyError = "" } diff --git a/agent/init/hook/hook.go b/agent/init/hook/hook.go index 3e6287dd7..d46272c07 100644 --- a/agent/init/hook/hook.go +++ b/agent/init/hook/hook.go @@ -24,6 +24,9 @@ func Init() { handleRecordStatus() handleSnapStatus() handleOllamaModelStatus() + if err := repo.NewIAgentAccountRepo().InterruptVerification(); err != nil { + global.LOG.Errorf("reset model service verification status failed: %v", err) + } loadLocalDir() diff --git a/agent/init/migration/migrate.go b/agent/init/migration/migrate.go index 4b96638cd..c2dc3ffc9 100644 --- a/agent/init/migration/migrate.go +++ b/agent/init/migration/migrate.go @@ -122,6 +122,7 @@ func agentDBMigrations() []*gormigrate.Migration { migrations.AddAgentAccountVerificationResult, migrations.AddAgentAccountType, migrations.AddGPUGroup, + migrations.AddAgentAccountStatus, } } diff --git a/agent/init/migration/migrations/init.go b/agent/init/migration/migrations/init.go index 6968f28e3..ba328cbbb 100644 --- a/agent/init/migration/migrations/init.go +++ b/agent/init/migration/migrations/init.go @@ -1234,6 +1234,9 @@ var NormalizeAgentAccountVerifiedStatus = &gormigrate.Migration{ if err := tx.AutoMigrate(&model.AgentAccount{}); err != nil { return err } + if !tx.Migrator().HasColumn(&model.AgentAccount{}, "verified") { + return nil + } return tx.Model(&model.AgentAccount{}). Where("provider IN ?", []string{"custom", "ollama", "kimi-coding"}). Update("verified", false).Error @@ -1956,3 +1959,29 @@ var AddCronjobURLItems = &gormigrate.Migration{ return tx.Migrator().AddColumn(&model.Cronjob{}, "URLItemsJSON") }, } + +var AddAgentAccountStatus = &gormigrate.Migration{ + ID: "20261116-add-agent-account-status", + Migrate: func(tx *gorm.DB) error { + if err := tx.AutoMigrate(&model.AgentAccount{}); err != nil { + return err + } + if err := tx.Model(&model.AgentAccount{}).Where("status IS NULL OR status = ?", "").Update("status", constant.StatusPending).Error; err != nil { + return err + } + if tx.Migrator().HasColumn(&model.AgentAccount{}, "verified") { + if err := tx.Model(&model.AgentAccount{}).Where("verified = ?", true).Update("status", constant.StatusSuccess).Error; err != nil { + return err + } + } + if tx.Migrator().HasColumn(&model.AgentAccount{}, "verify_error") { + if err := tx.Model(&model.AgentAccount{}).Where("verify_error <> ?", "").Update("status", constant.StatusFailed).Error; err != nil { + return err + } + } + if tx.Migrator().HasColumn(&model.AgentAccount{}, "verifying") { + return tx.Model(&model.AgentAccount{}).Where("verifying = ?", true).Update("status", constant.StatusWaiting).Error + } + return nil + }, +} diff --git a/frontend/src/api/interface/ai.ts b/frontend/src/api/interface/ai.ts index 5de89c46e..536523e75 100644 --- a/frontend/src/api/interface/ai.ts +++ b/frontend/src/api/interface/ai.ts @@ -795,7 +795,7 @@ export namespace AI { apiType: string; authMode: string; verifyModel: string; - verified: boolean; + status: string; validateAvailability: boolean; verifiedAt?: string | null; verifyError?: string; diff --git a/frontend/src/api/modules/ai.ts b/frontend/src/api/modules/ai.ts index ef9f60274..b61d8ad96 100644 --- a/frontend/src/api/modules/ai.ts +++ b/frontend/src/api/modules/ai.ts @@ -267,7 +267,7 @@ export const updateAgentAccountModel = (req: AI.AgentAccountModelUpdateReq) => { export const verifyAgentAccount = (req: { id: number }, currentNode: string) => { return http.postWithConfig(`/ai/accounts/verify`, req, { - timeout: TimeoutEnum.T_60S, + timeout: 20_000, errorMessageMode: 'manual', headers: { CurrentNode: currentNode }, }); diff --git a/frontend/src/views/ai/model/index.vue b/frontend/src/views/ai/model/index.vue index 31e4908a3..2336a6d76 100644 --- a/frontend/src/views/ai/model/index.vue +++ b/frontend/src/views/ai/model/index.vue @@ -122,7 +122,12 @@ :visible-count="1" > @@ -130,7 +135,7 @@ — - +