Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions backend/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "下一页标识",
Expand Down Expand Up @@ -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": "下一页标识",
Expand Down
15 changes: 11 additions & 4 deletions backend/domain/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 10 additions & 8 deletions backend/internal/billing/handler/http/v1/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
36 changes: 30 additions & 6 deletions backend/internal/billing/repo/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -54,15 +53,17 @@ 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().
WithTaskRecords().
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
}
Expand All @@ -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
}
Expand Down
10 changes: 4 additions & 6 deletions backend/internal/billing/usecase/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package usecase
import (
"context"

"github.com/GoYoko/web"

"github.com/chaitin/MonkeyCode/backend/domain"
)

Expand All @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions backend/internal/model/usecase/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading