fix(singleagent): create conversaion support add meta data field & ui… (#1949)

main
junwen-lee 2 months ago committed by GitHub
parent c74977e68c
commit 8c2d00f166
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      backend/api/handler/coze/conversation_service.go
  2. 37
      backend/application/conversation/conversation.go
  3. 2
      backend/application/conversation/openapi_agent_run.go
  4. 1
      backend/domain/conversation/agentrun/entity/run_record.go
  5. 8
      backend/domain/conversation/agentrun/internal/dal/dao.go
  6. 1
      backend/domain/conversation/conversation/entity/conversation.go
  7. 25
      backend/domain/conversation/conversation/internal/dal/dao.go
  8. 2
      backend/domain/conversation/conversation/repository/repository.go
  9. 2
      backend/domain/conversation/conversation/service/conversation_impl.go

@ -116,7 +116,7 @@ func CreateConversation(ctx context.Context, c *app.RequestContext) {
return return
} }
resp, err := application.ConversationSVC.CreateConversation(ctx, req.GetBotId(), req.GetConnectorId()) resp, err := application.ConversationSVC.CreateConversation(ctx, &req)
if err != nil { if err != nil {
internalServerErrorResponse(ctx, c, err) internalServerErrorResponse(ctx, c, err)
return return

@ -18,6 +18,8 @@ package conversation
import ( import (
"context" "context"
"encoding/json"
"strings"
"github.com/coze-dev/coze-studio/backend/api/model/conversation/common" "github.com/coze-dev/coze-studio/backend/api/model/conversation/common"
"github.com/coze-dev/coze-studio/backend/api/model/conversation/conversation" "github.com/coze-dev/coze-studio/backend/api/model/conversation/conversation"
@ -121,10 +123,12 @@ func (c *ConversationApplicationService) CreateSection(ctx context.Context, conv
return convRes.SectionID, nil return convRes.SectionID, nil
} }
func (c *ConversationApplicationService) CreateConversation(ctx context.Context, agentID int64, connectorID int64) (*conversation.CreateConversationResponse, error) { func (c *ConversationApplicationService) CreateConversation(ctx context.Context, req *conversation.CreateConversationRequest) (*conversation.CreateConversationResponse, error) {
resp := new(conversation.CreateConversationResponse) resp := new(conversation.CreateConversationResponse)
apiKeyInfo := ctxutil.GetApiAuthFromCtx(ctx) apiKeyInfo := ctxutil.GetApiAuthFromCtx(ctx)
userID := apiKeyInfo.UserID userID := apiKeyInfo.UserID
connectorID := req.GetConnectorId()
agentID := req.GetBotId()
if connectorID != consts.WebSDKConnectorID { if connectorID != consts.WebSDKConnectorID {
connectorID = apiKeyInfo.ConnectorID connectorID = apiKeyInfo.ConnectorID
} }
@ -134,6 +138,7 @@ func (c *ConversationApplicationService) CreateConversation(ctx context.Context,
UserID: userID, UserID: userID,
ConnectorID: connectorID, ConnectorID: connectorID,
Scene: common.Scene_SceneOpenApi, Scene: common.Scene_SceneOpenApi,
Ext: parseMetaData(req.MetaData),
}) })
if err != nil { if err != nil {
return nil, err return nil, err
@ -143,10 +148,33 @@ func (c *ConversationApplicationService) CreateConversation(ctx context.Context,
LastSectionID: &conversationData.SectionID, LastSectionID: &conversationData.SectionID,
ConnectorID: &conversationData.ConnectorID, ConnectorID: &conversationData.ConnectorID,
CreatedAt: conversationData.CreatedAt / 1000, CreatedAt: conversationData.CreatedAt / 1000,
MetaData: parseExt(conversationData.Ext),
} }
return resp, nil return resp, nil
} }
func parseMetaData(metaData map[string]string) string {
if metaData == nil {
return ""
}
j, err := json.Marshal(metaData)
if err != nil {
return ""
}
return string(j)
}
func parseExt(ext string) map[string]string {
if ext == "" {
return nil
}
var metaData map[string]string
err := json.Unmarshal([]byte(ext), &metaData)
if err != nil {
return nil
}
return metaData
}
func (c *ConversationApplicationService) ListConversation(ctx context.Context, req *conversation.ListConversationsApiRequest) (*conversation.ListConversationsApiResponse, error) { func (c *ConversationApplicationService) ListConversation(ctx context.Context, req *conversation.ListConversationsApiRequest) (*conversation.ListConversationsApiResponse, error) {
resp := new(conversation.ListConversationsApiResponse) resp := new(conversation.ListConversationsApiResponse)
@ -169,6 +197,12 @@ func (c *ConversationApplicationService) ListConversation(ctx context.Context, r
Scene: common.Scene_SceneOpenApi, Scene: common.Scene_SceneOpenApi,
Page: int(req.GetPageNum()), Page: int(req.GetPageNum()),
Limit: int(req.GetPageSize()), Limit: int(req.GetPageSize()),
OrderBy: func() *string {
if strings.ToLower(req.GetSortOrder()) == "asc" {
return ptr.Of("asc")
}
return nil
}(),
}) })
if err != nil { if err != nil {
return resp, err return resp, err
@ -180,6 +214,7 @@ func (c *ConversationApplicationService) ListConversation(ctx context.Context, r
ConnectorID: &conv.ConnectorID, ConnectorID: &conv.ConnectorID,
CreatedAt: conv.CreatedAt / 1000, CreatedAt: conv.CreatedAt / 1000,
Name: ptr.Of(conv.Name), Name: ptr.Of(conv.Name),
MetaData: parseExt(conv.Ext),
} }
}) })

@ -156,6 +156,8 @@ func (a *OpenapiAgentRunApplication) buildAgentRunRequest(ctx context.Context, a
ConnectorID: connectorID, ConnectorID: connectorID,
ContentType: contentType, ContentType: contentType,
Ext: ar.ExtraParams, Ext: ar.ExtraParams,
CustomVariables: ar.CustomVariables,
CozeUID: conversationData.CreatorID,
} }
return arm, nil return arm, nil
} }

@ -113,6 +113,7 @@ type AgentRunMeta struct {
SectionID int64 `json:"section_id"` SectionID int64 `json:"section_id"`
Name string `json:"name"` Name string `json:"name"`
UserID string `json:"user_id"` UserID string `json:"user_id"`
CozeUID int64 `json:"coze_uid"`
AgentID int64 `json:"agent_id"` AgentID int64 `json:"agent_id"`
ContentType message.ContentType `json:"content_type"` ContentType message.ContentType `json:"content_type"`
Content []*message.InputMetaData `json:"content"` Content []*message.InputMetaData `json:"content"`

@ -19,7 +19,6 @@ package dal
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"strconv"
"strings" "strings"
"time" "time"
@ -162,10 +161,7 @@ func (dao *RunRecordDAO) buildCreatePO(ctx context.Context, runMeta *entity.Agen
} }
timeNow := time.Now().UnixMilli() timeNow := time.Now().UnixMilli()
creatorID, err := strconv.ParseInt(runMeta.UserID, 10, 64)
if err != nil {
return nil, err
}
return &model.RunRecord{ return &model.RunRecord{
ID: runID, ID: runID,
ConversationID: runMeta.ConversationID, ConversationID: runMeta.ConversationID,
@ -175,7 +171,7 @@ func (dao *RunRecordDAO) buildCreatePO(ctx context.Context, runMeta *entity.Agen
ChatRequest: string(reqOrigin), ChatRequest: string(reqOrigin),
UserID: runMeta.UserID, UserID: runMeta.UserID,
CreatedAt: timeNow, CreatedAt: timeNow,
CreatorID: creatorID, CreatorID: runMeta.CozeUID,
}, nil }, nil
} }

@ -50,6 +50,7 @@ type ListMeta struct {
AgentID int64 `json:"agent_id"` AgentID int64 `json:"agent_id"`
Limit int `json:"limit"` Limit int `json:"limit"`
Page int `json:"page"` Page int `json:"page"`
OrderBy *string `json:"order_by"`
} }
type UpdateMeta struct { type UpdateMeta struct {

@ -29,6 +29,7 @@ import (
"github.com/coze-dev/coze-studio/backend/domain/conversation/conversation/internal/dal/model" "github.com/coze-dev/coze-studio/backend/domain/conversation/conversation/internal/dal/model"
"github.com/coze-dev/coze-studio/backend/domain/conversation/conversation/internal/dal/query" "github.com/coze-dev/coze-studio/backend/domain/conversation/conversation/internal/dal/query"
"github.com/coze-dev/coze-studio/backend/infra/contract/idgen" "github.com/coze-dev/coze-studio/backend/infra/contract/idgen"
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr"
"github.com/coze-dev/coze-studio/backend/pkg/lang/slices" "github.com/coze-dev/coze-studio/backend/pkg/lang/slices"
) )
@ -140,22 +141,26 @@ func (dao *ConversationDAO) Get(ctx context.Context, userID int64, agentID int64
return dao.conversationPO2DO(ctx, po), nil return dao.conversationPO2DO(ctx, po), nil
} }
func (dao *ConversationDAO) List(ctx context.Context, userID int64, agentID int64, connectorID int64, scene int32, limit int, page int) ([]*entity.Conversation, bool, error) { func (dao *ConversationDAO) List(ctx context.Context, listMeta *entity.ListMeta) ([]*entity.Conversation, bool, error) {
var hasMore bool var hasMore bool
do := dao.query.Conversation.WithContext(ctx).Debug() do := dao.query.Conversation.WithContext(ctx).Debug()
do = do.Where(dao.query.Conversation.CreatorID.Eq(userID)). do = do.Where(dao.query.Conversation.CreatorID.Eq(listMeta.UserID)).
Where(dao.query.Conversation.AgentID.Eq(agentID)). Where(dao.query.Conversation.AgentID.Eq(listMeta.AgentID)).
Where(dao.query.Conversation.Scene.Eq(scene)). Where(dao.query.Conversation.Scene.Eq(int32(listMeta.Scene))).
Where(dao.query.Conversation.ConnectorID.Eq(connectorID)). Where(dao.query.Conversation.ConnectorID.Eq(listMeta.ConnectorID)).
Where(dao.query.Conversation.Status.Eq(int32(conversation.ConversationStatusNormal))) Where(dao.query.Conversation.Status.Eq(int32(conversation.ConversationStatusNormal)))
do = do.Offset((page - 1) * limit) do = do.Offset((listMeta.Page - 1) * listMeta.Limit)
if limit > 0 { if listMeta.Limit > 0 {
do = do.Limit(int(limit) + 1) do = do.Limit(int(listMeta.Limit) + 1)
}
if listMeta.OrderBy != nil && ptr.From(listMeta.OrderBy) == "asc" {
do = do.Order(dao.query.Conversation.CreatedAt.Asc())
} else {
do = do.Order(dao.query.Conversation.CreatedAt.Desc())
} }
do = do.Order(dao.query.Conversation.CreatedAt.Desc())
poList, err := do.Find() poList, err := do.Find()
@ -169,7 +174,7 @@ func (dao *ConversationDAO) List(ctx context.Context, userID int64, agentID int6
if len(poList) == 0 { if len(poList) == 0 {
return nil, hasMore, nil return nil, hasMore, nil
} }
if len(poList) > limit { if len(poList) > listMeta.Limit {
hasMore = true hasMore = true
return dao.conversationBatchPO2DO(ctx, poList[:(len(poList)-1)]), hasMore, nil return dao.conversationBatchPO2DO(ctx, poList[:(len(poList)-1)]), hasMore, nil

@ -37,5 +37,5 @@ type ConversationRepo interface {
Get(ctx context.Context, userID int64, agentID int64, scene int32, connectorID int64) (*entity.Conversation, error) Get(ctx context.Context, userID int64, agentID int64, scene int32, connectorID int64) (*entity.Conversation, error)
Update(ctx context.Context, req *entity.UpdateMeta) (*entity.Conversation, error) Update(ctx context.Context, req *entity.UpdateMeta) (*entity.Conversation, error)
Delete(ctx context.Context, id int64) (int64, error) Delete(ctx context.Context, id int64) (int64, error)
List(ctx context.Context, userID int64, agentID int64, connectorID int64, scene int32, limit int, page int) ([]*entity.Conversation, bool, error) List(ctx context.Context, req *entity.ListMeta) ([]*entity.Conversation, bool, error)
} }

@ -107,7 +107,7 @@ func (c *conversationImpl) Update(ctx context.Context, req *entity.UpdateMeta) (
} }
func (c *conversationImpl) List(ctx context.Context, req *entity.ListMeta) ([]*entity.Conversation, bool, error) { func (c *conversationImpl) List(ctx context.Context, req *entity.ListMeta) ([]*entity.Conversation, bool, error) {
conversationList, hasMore, err := c.ConversationRepo.List(ctx, req.UserID, req.AgentID, req.ConnectorID, int32(req.Scene), req.Limit, req.Page) conversationList, hasMore, err := c.ConversationRepo.List(ctx, req)
if err != nil { if err != nil {
return nil, hasMore, err return nil, hasMore, err

Loading…
Cancel
Save