Skip to content

Commit 30bf4a8

Browse files
feat: add agent website check for delete (#12433)
1 parent 9e501ce commit 30bf4a8

6 files changed

Lines changed: 89 additions & 2 deletions

File tree

agent/app/api/v2/agents.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,27 @@ func (b *BaseApi) PageAgents(c *gin.Context) {
5151
})
5252
}
5353

54+
// @Tags AI
55+
// @Summary Delete check Agent
56+
// @Accept json
57+
// @Param request body dto.AgentIDReq true "request"
58+
// @Success 200 {array} dto.AppResource
59+
// @Security ApiKeyAuth
60+
// @Security Timestamp
61+
// @Router /ai/agents/delete/check [post]
62+
func (b *BaseApi) DeleteCheckAgent(c *gin.Context) {
63+
var req dto.AgentIDReq
64+
if err := helper.CheckBindAndValidate(&req, c); err != nil {
65+
return
66+
}
67+
checkData, err := agentService.DeleteCheck(req)
68+
if err != nil {
69+
helper.BadRequest(c, err)
70+
return
71+
}
72+
helper.SuccessWithData(c, checkData)
73+
}
74+
5475
// @Tags AI
5576
// @Summary Delete Agent
5677
// @Accept json

agent/app/service/agents.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
type IAgentService interface {
2929
Create(req dto.AgentCreateReq) (*dto.AgentItem, error)
3030
Page(req dto.SearchWithPage) (int64, []dto.AgentItem, error)
31+
DeleteCheck(req dto.AgentIDReq) ([]dto.AppResource, error)
3132
Delete(req dto.AgentDeleteReq) error
3233
ResetToken(req dto.AgentTokenResetReq) error
3334
UpdateRemark(req dto.AgentRemarkUpdateReq) error
@@ -301,6 +302,13 @@ func (a AgentService) Delete(req dto.AgentDeleteReq) error {
301302
if err != nil {
302303
return err
303304
}
305+
resources, err := a.deleteCheckByAgent(agent)
306+
if err != nil {
307+
return err
308+
}
309+
if len(resources) > 0 {
310+
return buserr.New("ErrAgentWebsiteBound")
311+
}
304312
if agent.AppInstallID == 0 {
305313
return agentRepo.DeleteByID(agent.ID)
306314
}
@@ -316,6 +324,32 @@ func (a AgentService) Delete(req dto.AgentDeleteReq) error {
316324
return nil
317325
}
318326

327+
func (a AgentService) DeleteCheck(req dto.AgentIDReq) ([]dto.AppResource, error) {
328+
agent, err := agentRepo.GetFirst(repo.WithByID(req.AgentID))
329+
if err != nil {
330+
return nil, err
331+
}
332+
return a.deleteCheckByAgent(agent)
333+
}
334+
335+
func (a AgentService) deleteCheckByAgent(agent *model.Agent) ([]dto.AppResource, error) {
336+
if agent == nil || agent.WebsiteID == 0 {
337+
return nil, nil
338+
}
339+
website, err := websiteRepo.GetFirst(repo.WithByID(agent.WebsiteID))
340+
if err != nil {
341+
if err == gorm.ErrRecordNotFound {
342+
return nil, nil
343+
}
344+
return nil, err
345+
}
346+
websiteName, err := loadAgentWebsiteResourceName(website)
347+
if err != nil {
348+
return nil, err
349+
}
350+
return []dto.AppResource{{Type: "website", Name: websiteName}}, nil
351+
}
352+
319353
func (a AgentService) ResetToken(req dto.AgentTokenResetReq) error {
320354
agent, err := loadOpenclawAgentByID(req.ID)
321355
if err != nil {

agent/app/service/agents_website.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,20 @@ func loadAgentWebsiteDomainMapByWebsiteID(items []dto.AgentItem) (map[uint][]mod
103103
return websiteDomainMap, nil
104104
}
105105

106+
func loadAgentWebsiteResourceName(website model.Website) (string, error) {
107+
websiteDomains, err := websiteDomainRepo.GetBy(websiteDomainRepo.WithWebsiteId(website.ID), repo.WithOrderAsc("id"))
108+
if err != nil {
109+
return "", err
110+
}
111+
if len(websiteDomains) > 0 {
112+
return websiteDomains[0].Domain, nil
113+
}
114+
if website.PrimaryDomain != "" {
115+
return website.PrimaryDomain, nil
116+
}
117+
return fmt.Sprintf("%d", website.ID), nil
118+
}
119+
106120
func fillAgentWebsiteItems(items []dto.AgentItem, explicitWebsiteMap map[uint]model.Website, websiteDomainMap map[uint][]model.WebsiteDomain) {
107121
for index := range items {
108122
if items[index].WebsiteID == 0 {

agent/router/ro_ai.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func (a *AIToolsRouter) InitRouter(Router *gin.RouterGroup) {
4242

4343
aiToolsRouter.POST("/agents", baseApi.CreateAgent)
4444
aiToolsRouter.POST("/agents/search", baseApi.PageAgents)
45+
aiToolsRouter.POST("/agents/delete/check", baseApi.DeleteCheckAgent)
4546
aiToolsRouter.POST("/agents/delete", baseApi.DeleteAgent)
4647
aiToolsRouter.POST("/agents/token/reset", baseApi.ResetAgentToken)
4748
aiToolsRouter.POST("/agents/remark", baseApi.UpdateAgentRemark)

frontend/src/api/modules/ai.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { AI } from '@/api/interface/ai';
2+
import { App } from '@/api/interface/app';
23
import http from '@/api';
34
import { ResPage, SearchWithPage } from '../interface';
45
import { TimeoutEnum } from '@/enums/http-enum';
@@ -101,6 +102,10 @@ export const pageAgents = (req: SearchWithPage) => {
101102
return http.post<ResPage<AI.AgentItem>>(`/ai/agents/search`, req);
102103
};
103104

105+
export const deleteAgentCheck = (req: AI.AgentIDReq) => {
106+
return http.post<App.AppInstallResource[]>(`/ai/agents/delete/check`, req);
107+
};
108+
104109
export const deleteAgent = (req: AI.AgentDeleteReq) => {
105110
return http.post(`/ai/agents/delete`, req);
106111
};

frontend/src/views/ai/agents/agent/index.vue

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
<AddDialog ref="addRef" @search="search" @task="openTaskLog" />
191191
<TaskLog ref="taskLogRef" @close="search" />
192192
<DeleteDialog ref="deleteRef" @close="search" />
193+
<AppResources ref="checkRef" @close="search" />
193194
<ConfigDrawer ref="configRef" @updated="search" />
194195
<OverviewDrawer ref="overviewRef" />
195196
<BindWebsiteDialog ref="bindWebsiteRef" @success="search" />
@@ -203,7 +204,7 @@
203204
<script setup lang="ts">
204205
import { onMounted, reactive, ref } from 'vue';
205206
import { useRoute, useRouter } from 'vue-router';
206-
import { pageAgents, resetAgentToken, updateAgentRemark } from '@/api/modules/ai';
207+
import { deleteAgentCheck, pageAgents, resetAgentToken, updateAgentRemark } from '@/api/modules/ai';
207208
import { checkAppInstalled, installedOp, searchApp, searchAppInstalled } from '@/api/modules/app';
208209
import { AI } from '@/api/interface/ai';
209210
import { App } from '@/api/interface/app';
@@ -214,6 +215,7 @@ import { MsgSuccess } from '@/utils/message';
214215
215216
import AddDialog from '@/views/ai/agents/agent/add/index.vue';
216217
import DeleteDialog from '@/views/ai/agents/agent/delete/index.vue';
218+
import AppResources from '@/views/app-store/installed/check/index.vue';
217219
import ConfigDrawer from '@/views/ai/agents/agent/config/index.vue';
218220
import OverviewDrawer from '@/views/ai/agents/agent/components/overview.vue';
219221
import BindWebsiteDialog from '@/views/ai/agents/agent/website/index.vue';
@@ -236,6 +238,7 @@ const loading = ref(false);
236238
const addRef = ref();
237239
const taskLogRef = ref();
238240
const deleteRef = ref();
241+
const checkRef = ref();
239242
const configRef = ref();
240243
const overviewRef = ref();
241244
const bindWebsiteRef = ref();
@@ -442,7 +445,16 @@ const jumpWebUI = (row: AI.AgentItem) => {
442445
}
443446
};
444447
445-
const onDelete = (row: AI.AgentItem) => {
448+
const onDelete = async (row: AI.AgentItem) => {
449+
const res = await deleteAgentCheck({ agentId: row.id });
450+
if ((res.data || []).length > 0) {
451+
checkRef.value?.acceptParams({
452+
items: res.data,
453+
installID: row.appInstallId,
454+
key: row.agentType,
455+
});
456+
return;
457+
}
446458
deleteRef.value?.acceptParams(row.id, row.name);
447459
};
448460

0 commit comments

Comments
 (0)