From b8e5011d06faedbc7bbceebb336ef1486a7e4461 Mon Sep 17 00:00:00 2001 From: yokowu <18836617@qq.com> Date: Wed, 2 Jul 2025 17:23:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=AE=B0=E5=BD=95=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E7=AD=9B=E9=80=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/docs/swagger.json | 36 +++++++++++++++++++ backend/domain/billing.go | 15 +++++--- .../billing/handler/http/v1/billing.go | 18 +++++----- backend/internal/billing/repo/billing.go | 36 +++++++++++++++---- backend/internal/billing/usecase/billing.go | 10 +++--- backend/internal/model/usecase/model.go | 3 ++ 6 files changed, 94 insertions(+), 24 deletions(-) diff --git a/backend/docs/swagger.json b/backend/docs/swagger.json index d0d438c..5c329e4 100644 --- a/backend/docs/swagger.json +++ b/backend/docs/swagger.json @@ -399,6 +399,24 @@ "summary": "获取对话记录", "operationId": "list-chat-record", "parameters": [ + { + "type": "string", + "description": "作者", + "name": "author", + "in": "query" + }, + { + "type": "boolean", + "description": "是否接受筛选", + "name": "is_accept", + "in": "query" + }, + { + "type": "string", + "description": "语言", + "name": "language", + "in": "query" + }, { "type": "string", "description": "下一页标识", @@ -500,6 +518,24 @@ "summary": "获取补全记录", "operationId": "list-completion-record", "parameters": [ + { + "type": "string", + "description": "作者", + "name": "author", + "in": "query" + }, + { + "type": "boolean", + "description": "是否接受筛选", + "name": "is_accept", + "in": "query" + }, + { + "type": "string", + "description": "语言", + "name": "language", + "in": "query" + }, { "type": "string", "description": "下一页标识", diff --git a/backend/domain/billing.go b/backend/domain/billing.go index ecbf9ae..290910f 100644 --- a/backend/domain/billing.go +++ b/backend/domain/billing.go @@ -10,19 +10,26 @@ import ( ) type BillingUsecase interface { - ListChatRecord(ctx context.Context, page *web.Pagination) (*ListChatRecordResp, error) - ListCompletionRecord(ctx context.Context, page *web.Pagination) (*ListCompletionRecordResp, error) + ListChatRecord(ctx context.Context, req ListRecordReq) (*ListChatRecordResp, error) + ListCompletionRecord(ctx context.Context, req ListRecordReq) (*ListCompletionRecordResp, error) CompletionInfo(ctx context.Context, id string) (*CompletionInfo, error) ChatInfo(ctx context.Context, id string) (*ChatInfo, error) } type BillingRepo interface { - ListChatRecord(ctx context.Context, page *web.Pagination) (*ListChatRecordResp, error) - ListCompletionRecord(ctx context.Context, page *web.Pagination) (*ListCompletionRecordResp, error) + ListChatRecord(ctx context.Context, req ListRecordReq) (*ListChatRecordResp, error) + ListCompletionRecord(ctx context.Context, req ListRecordReq) (*ListCompletionRecordResp, error) CompletionInfo(ctx context.Context, id string) (*CompletionInfo, error) ChatInfo(ctx context.Context, id string) (*ChatInfo, error) } +type ListRecordReq struct { + *web.Pagination + Author string `json:"author" query:"author"` // 作者 + Language string `json:"language" query:"language"` // 语言 + IsAccept *bool `json:"is_accept" query:"is_accept"` // 是否接受筛选 +} + type ListChatRecordResp struct { *db.PageInfo diff --git a/backend/internal/billing/handler/http/v1/billing.go b/backend/internal/billing/handler/http/v1/billing.go index ef59ea4..0b430e4 100644 --- a/backend/internal/billing/handler/http/v1/billing.go +++ b/backend/internal/billing/handler/http/v1/billing.go @@ -23,8 +23,8 @@ func NewBillingHandler( g := w.Group("/api/v1/billing") g.Use(auth.Auth()) - g.GET("/chat/record", web.BaseHandler(b.ListChatRecord, web.WithPage())) - g.GET("/completion/record", web.BaseHandler(b.ListCompletionRecord, web.WithPage())) + g.GET("/chat/record", web.BindHandler(b.ListChatRecord, web.WithPage())) + g.GET("/completion/record", web.BindHandler(b.ListCompletionRecord, web.WithPage())) g.GET("/completion/info", web.BaseHandler(b.CompletionInfo)) g.GET("/chat/info", web.BaseHandler(b.ChatInfo)) @@ -39,11 +39,12 @@ func NewBillingHandler( // @ID list-chat-record // @Accept json // @Produce json -// @Param page query web.Pagination true "分页" +// @Param page query domain.ListRecordReq true "参数" // @Success 200 {object} web.Resp{data=domain.ListChatRecordResp} // @Router /api/v1/billing/chat/record [get] -func (h *BillingHandler) ListChatRecord(c *web.Context) error { - records, err := h.usecase.ListChatRecord(c.Request().Context(), c.Page()) +func (h *BillingHandler) ListChatRecord(c *web.Context, req domain.ListRecordReq) error { + req.Pagination = c.Page() + records, err := h.usecase.ListChatRecord(c.Request().Context(), req) if err != nil { return err } @@ -58,11 +59,12 @@ func (h *BillingHandler) ListChatRecord(c *web.Context) error { // @ID list-completion-record // @Accept json // @Produce json -// @Param page query web.Pagination true "分页" +// @Param page query domain.ListRecordReq true "参数" // @Success 200 {object} web.Resp{data=domain.ListCompletionRecordResp} // @Router /api/v1/billing/completion/record [get] -func (h *BillingHandler) ListCompletionRecord(c *web.Context) error { - records, err := h.usecase.ListCompletionRecord(c.Request().Context(), c.Page()) +func (h *BillingHandler) ListCompletionRecord(c *web.Context, req domain.ListRecordReq) error { + req.Pagination = c.Page() + records, err := h.usecase.ListCompletionRecord(c.Request().Context(), req) if err != nil { return err } diff --git a/backend/internal/billing/repo/billing.go b/backend/internal/billing/repo/billing.go index 2d007d5..fa55349 100644 --- a/backend/internal/billing/repo/billing.go +++ b/backend/internal/billing/repo/billing.go @@ -5,12 +5,11 @@ import ( "entgo.io/ent/dialect/sql" - "github.com/GoYoko/web" - "github.com/chaitin/MonkeyCode/backend/consts" "github.com/chaitin/MonkeyCode/backend/db" "github.com/chaitin/MonkeyCode/backend/db/task" "github.com/chaitin/MonkeyCode/backend/db/taskrecord" + "github.com/chaitin/MonkeyCode/backend/db/user" "github.com/chaitin/MonkeyCode/backend/domain" "github.com/chaitin/MonkeyCode/backend/pkg/cvt" ) @@ -54,7 +53,7 @@ func (b *BillingRepo) CompletionInfo(ctx context.Context, id string) (*domain.Co } // ListChatRecord implements domain.BillingRepo. -func (b *BillingRepo) ListChatRecord(ctx context.Context, page *web.Pagination) (*domain.ListChatRecordResp, error) { +func (b *BillingRepo) ListChatRecord(ctx context.Context, req domain.ListRecordReq) (*domain.ListChatRecordResp, error) { q := b.db.Task.Query(). WithUser(). WithModel(). @@ -62,7 +61,9 @@ func (b *BillingRepo) ListChatRecord(ctx context.Context, page *web.Pagination) Where(task.ModelType(consts.ModelTypeLLM)). Order(task.ByCreatedAt(sql.OrderDesc())) - records, p, err := q.Page(ctx, page.Page, page.Size) + filterTask(q, req) + + records, p, err := q.Page(ctx, req.Page, req.Size) if err != nil { return nil, err } @@ -75,14 +76,37 @@ func (b *BillingRepo) ListChatRecord(ctx context.Context, page *web.Pagination) }, nil } +func filterTask(q *db.TaskQuery, req domain.ListRecordReq) { + if req.IsAccept != nil { + q.Where(task.IsAccept(*req.IsAccept)) + } + + if req.Author != "" { + q.Where(task.HasUserWith(func(s *sql.Selector) { + s.Where(sql.Like(s.C(user.FieldUsername), "%"+req.Author+"%")) + })) + } + + if req.Language != "" { + q.Where(func(s *sql.Selector) { + s.Where( + sql.Like(s.C(task.FieldProgramLanguage), "%"+req.Language+"%"), + ) + }) + } +} + // ListCompletionRecord implements domain.BillingRepo. -func (b *BillingRepo) ListCompletionRecord(ctx context.Context, page *web.Pagination) (*domain.ListCompletionRecordResp, error) { +func (b *BillingRepo) ListCompletionRecord(ctx context.Context, req domain.ListRecordReq) (*domain.ListCompletionRecordResp, error) { q := b.db.Task.Query(). WithUser(). WithModel(). Where(task.ModelType(consts.ModelTypeCoder)). Order(task.ByCreatedAt(sql.OrderDesc())) - records, p, err := q.Page(ctx, page.Page, page.Size) + + filterTask(q, req) + + records, p, err := q.Page(ctx, req.Page, req.Size) if err != nil { return nil, err } diff --git a/backend/internal/billing/usecase/billing.go b/backend/internal/billing/usecase/billing.go index ba1530d..7aa7599 100644 --- a/backend/internal/billing/usecase/billing.go +++ b/backend/internal/billing/usecase/billing.go @@ -3,8 +3,6 @@ package usecase import ( "context" - "github.com/GoYoko/web" - "github.com/chaitin/MonkeyCode/backend/domain" ) @@ -17,13 +15,13 @@ func NewBillingUsecase(repo domain.BillingRepo) domain.BillingUsecase { } // ListChatRecord implements domain.BillingUsecase. -func (b *BillingUsecase) ListChatRecord(ctx context.Context, page *web.Pagination) (*domain.ListChatRecordResp, error) { - return b.repo.ListChatRecord(ctx, page) +func (b *BillingUsecase) ListChatRecord(ctx context.Context, req domain.ListRecordReq) (*domain.ListChatRecordResp, error) { + return b.repo.ListChatRecord(ctx, req) } // ListCompletionRecord implements domain.BillingUsecase. -func (b *BillingUsecase) ListCompletionRecord(ctx context.Context, page *web.Pagination) (*domain.ListCompletionRecordResp, error) { - return b.repo.ListCompletionRecord(ctx, page) +func (b *BillingUsecase) ListCompletionRecord(ctx context.Context, req domain.ListRecordReq) (*domain.ListCompletionRecordResp, error) { + return b.repo.ListCompletionRecord(ctx, req) } // CompletionInfo implements domain.BillingUsecase. diff --git a/backend/internal/model/usecase/model.go b/backend/internal/model/usecase/model.go index 9f13027..5a2d5cc 100644 --- a/backend/internal/model/usecase/model.go +++ b/backend/internal/model/usecase/model.go @@ -109,5 +109,8 @@ func (m *ModelUsecase) Update(ctx context.Context, req *domain.UpdateModelReq) ( func (m *ModelUsecase) InitModel(ctx context.Context) error { m.logger.With("init_model", m.cfg.InitModel).Debug("init model") + if m.cfg.InitModel.ModelName == "" { + return nil + } return m.repo.InitModel(ctx, m.cfg.InitModel.ModelName, m.cfg.InitModel.ModelKey, m.cfg.InitModel.ModelURL) }