refactor(workflow): Calculate chat history rounds during schema convertion (#1990)

Co-authored-by: zhuangjie.1125 <zhuangjie.1125@bytedance.com>
This commit is contained in:
lvxinyu-1117
2025-09-10 15:52:23 +08:00
committed by GitHub
parent 4416127d47
commit 4bfce5a8cb
29 changed files with 2492 additions and 384 deletions

View File

@ -221,7 +221,7 @@ const (
"id": "5fJt3qKpSz",
"name": "list",
"defaultValue": [
]
}
},
@ -504,7 +504,7 @@ func (w *ApplicationService) OpenAPIChatFlowRun(ctx context.Context, req *workfl
workflowID = mustParseInt64(req.GetWorkflowID())
isDebug = req.GetExecuteMode() == "DEBUG"
appID, agentID *int64
resolveAppID int64
bizID int64
conversationID int64
sectionID int64
version string
@ -521,11 +521,11 @@ func (w *ApplicationService) OpenAPIChatFlowRun(ctx context.Context, req *workfl
if req.IsSetAppID() {
appID = ptr.Of(mustParseInt64(req.GetAppID()))
resolveAppID = mustParseInt64(req.GetAppID())
bizID = mustParseInt64(req.GetAppID())
}
if req.IsSetBotID() {
agentID = ptr.Of(mustParseInt64(req.GetBotID()))
resolveAppID = mustParseInt64(req.GetBotID())
bizID = mustParseInt64(req.GetBotID())
}
if appID != nil && agentID != nil {
@ -564,16 +564,16 @@ func (w *ApplicationService) OpenAPIChatFlowRun(ctx context.Context, req *workfl
sectionID = cInfo.SectionID
// only trust the conversation name under the app
conversationName, existed, err := GetWorkflowDomainSVC().GetConversationNameByID(ctx, ternary.IFElse(isDebug, vo.Draft, vo.Online), resolveAppID, connectorID, conversationID)
conversationName, existed, err := GetWorkflowDomainSVC().GetConversationNameByID(ctx, ternary.IFElse(isDebug, vo.Draft, vo.Online), bizID, connectorID, conversationID)
if err != nil {
return nil, err
}
if !existed {
return nil, fmt.Errorf("conversation not found")
}
parameters["CONVERSATION_NAME"] = conversationName
parameters[vo.ConversationNameKey] = conversationName
} else if req.IsSetConversationID() && req.IsSetBotID() {
parameters["CONVERSATION_NAME"] = "Default"
parameters[vo.ConversationNameKey] = "Default"
conversationID = mustParseInt64(req.GetConversationID())
cInfo, err := crossconversation.DefaultSVC().GetByID(ctx, conversationID)
if err != nil {
@ -581,11 +581,11 @@ func (w *ApplicationService) OpenAPIChatFlowRun(ctx context.Context, req *workfl
}
sectionID = cInfo.SectionID
} else {
conversationName, ok := parameters["CONVERSATION_NAME"].(string)
conversationName, ok := parameters[vo.ConversationNameKey].(string)
if !ok {
return nil, fmt.Errorf("conversation name is requried")
}
cID, sID, err := GetWorkflowDomainSVC().GetOrCreateConversation(ctx, ternary.IFElse(isDebug, vo.Draft, vo.Online), resolveAppID, connectorID, userID, conversationName)
cID, sID, err := GetWorkflowDomainSVC().GetOrCreateConversation(ctx, ternary.IFElse(isDebug, vo.Draft, vo.Online), bizID, connectorID, userID, conversationName)
if err != nil {
return nil, err
}
@ -594,7 +594,7 @@ func (w *ApplicationService) OpenAPIChatFlowRun(ctx context.Context, req *workfl
}
runRecord, err := crossagentrun.DefaultSVC().Create(ctx, &agententity.AgentRunMeta{
AgentID: resolveAppID,
AgentID: bizID,
ConversationID: conversationID,
UserID: strconv.FormatInt(userID, 10),
ConnectorID: connectorID,
@ -606,7 +606,7 @@ func (w *ApplicationService) OpenAPIChatFlowRun(ctx context.Context, req *workfl
roundID := runRecord.ID
userMessage, err := toConversationMessage(ctx, resolveAppID, conversationID, userID, roundID, sectionID, message.MessageTypeQuestion, lastUserMessage)
userMessage, err := toConversationMessage(ctx, bizID, conversationID, userID, roundID, sectionID, message.MessageTypeQuestion, lastUserMessage)
if err != nil {
return nil, err
}
@ -648,7 +648,7 @@ func (w *ApplicationService) OpenAPIChatFlowRun(ctx context.Context, req *workfl
return nil, err
}
return schema.StreamReaderWithConvert(sr, w.convertToChatFlowRunResponseList(ctx, convertToChatFlowInfo{
appID: resolveAppID,
bizID: bizID,
conversationID: conversationID,
roundID: roundID,
workflowID: workflowID,
@ -684,7 +684,7 @@ func (w *ApplicationService) OpenAPIChatFlowRun(ctx context.Context, req *workfl
Cancellable: isDebug,
}
historyMessages, err := makeChatFlowHistoryMessages(ctx, resolveAppID, conversationID, userID, sectionID, connectorID, messages[:len(req.GetAdditionalMessages())-1])
historyMessages, err := makeChatFlowHistoryMessages(ctx, bizID, conversationID, userID, sectionID, connectorID, messages[:len(req.GetAdditionalMessages())-1])
if err != nil {
return nil, err
}
@ -706,7 +706,7 @@ func (w *ApplicationService) OpenAPIChatFlowRun(ctx context.Context, req *workfl
logs.CtxWarnf(ctx, "create history message failed, err=%v", err)
}
}
parameters["USER_INPUT"], err = w.makeChatFlowUserInput(ctx, lastUserMessage)
parameters[vo.UserInputKey], err = w.makeChatFlowUserInput(ctx, lastUserMessage)
if err != nil {
return nil, err
}
@ -717,7 +717,7 @@ func (w *ApplicationService) OpenAPIChatFlowRun(ctx context.Context, req *workfl
}
return schema.StreamReaderWithConvert(sr, w.convertToChatFlowRunResponseList(ctx, convertToChatFlowInfo{
appID: resolveAppID,
bizID: bizID,
conversationID: conversationID,
roundID: roundID,
workflowID: workflowID,
@ -731,7 +731,7 @@ func (w *ApplicationService) OpenAPIChatFlowRun(ctx context.Context, req *workfl
func (w *ApplicationService) convertToChatFlowRunResponseList(ctx context.Context, info convertToChatFlowInfo) func(msg *entity.Message) (responses []*workflow.ChatFlowRunResponse, err error) {
var (
appID = info.appID
bizID = info.bizID
conversationID = info.conversationID
roundID = info.roundID
workflowID = info.workflowID
@ -798,7 +798,7 @@ func (w *ApplicationService) convertToChatFlowRunResponseList(ctx context.Contex
ChatID: strconv.FormatInt(roundID, 10),
ConversationID: strconv.FormatInt(conversationID, 10),
SectionID: strconv.FormatInt(sectionID, 10),
BotID: strconv.FormatInt(appID, 10),
BotID: strconv.FormatInt(bizID, 10),
Role: string(schema.Assistant),
Type: "follow_up",
ContentType: "text",
@ -815,7 +815,7 @@ func (w *ApplicationService) convertToChatFlowRunResponseList(ctx context.Contex
ID: strconv.FormatInt(roundID, 10),
ConversationID: strconv.FormatInt(conversationID, 10),
SectionID: strconv.FormatInt(sectionID, 10),
BotID: strconv.FormatInt(appID, 10),
BotID: strconv.FormatInt(bizID, 10),
Status: vo.Completed,
ExecuteID: strconv.FormatInt(executeID, 10),
Usage: &vo.Usage{
@ -929,7 +929,7 @@ func (w *ApplicationService) convertToChatFlowRunResponseList(ctx context.Contex
}
_, err = crossmessage.DefaultSVC().Create(ctx, &message.Message{
AgentID: appID,
AgentID: bizID,
RunID: roundID,
SectionID: sectionID,
Content: msgContent,
@ -947,7 +947,7 @@ func (w *ApplicationService) convertToChatFlowRunResponseList(ctx context.Contex
ChatID: strconv.FormatInt(roundID, 10),
ConversationID: strconv.FormatInt(conversationID, 10),
SectionID: strconv.FormatInt(sectionID, 10),
BotID: strconv.FormatInt(appID, 10),
BotID: strconv.FormatInt(bizID, 10),
Role: string(schema.Assistant),
Type: string(entity.Answer),
ContentType: string(contentType),
@ -1046,7 +1046,7 @@ func (w *ApplicationService) convertToChatFlowRunResponseList(ctx context.Contex
}
intermediateMessage = &message.Message{
ID: id,
AgentID: appID,
AgentID: bizID,
RunID: roundID,
SectionID: sectionID,
ConversationID: conversationID,
@ -1066,7 +1066,7 @@ func (w *ApplicationService) convertToChatFlowRunResponseList(ctx context.Contex
ChatID: strconv.FormatInt(roundID, 10),
ConversationID: strconv.FormatInt(conversationID, 10),
SectionID: strconv.FormatInt(sectionID, 10),
BotID: strconv.FormatInt(appID, 10),
BotID: strconv.FormatInt(bizID, 10),
Role: string(dataMessage.Role),
Type: string(dataMessage.Type),
ContentType: string(message.ContentTypeText),
@ -1092,7 +1092,7 @@ func (w *ApplicationService) convertToChatFlowRunResponseList(ctx context.Contex
ChatID: strconv.FormatInt(roundID, 10),
ConversationID: strconv.FormatInt(conversationID, 10),
SectionID: strconv.FormatInt(sectionID, 10),
BotID: strconv.FormatInt(appID, 10),
BotID: strconv.FormatInt(bizID, 10),
Role: string(dataMessage.Role),
Type: string(dataMessage.Type),
ContentType: string(message.ContentTypeText),
@ -1155,9 +1155,9 @@ func (w *ApplicationService) makeChatFlowUserInput(ctx context.Context, message
} else {
return "", fmt.Errorf("invalid message ccontent type %v", message.ContentType)
}
}
func makeChatFlowHistoryMessages(ctx context.Context, appID, conversationID, userID, sectionID, connectorID int64, messages []*workflow.EnterMessage) ([]*message.Message, error) {
func makeChatFlowHistoryMessages(ctx context.Context, bizID, conversationID, userID, sectionID, connectorID int64, messages []*workflow.EnterMessage) ([]*message.Message, error) {
var (
rID int64
@ -1170,7 +1170,7 @@ func makeChatFlowHistoryMessages(ctx context.Context, appID, conversationID, use
for _, msg := range messages {
if msg.Role == userRole {
runRecord, err = crossagentrun.DefaultSVC().Create(ctx, &agententity.AgentRunMeta{
AgentID: appID,
AgentID: bizID,
ConversationID: conversationID,
UserID: strconv.FormatInt(userID, 10),
ConnectorID: connectorID,
@ -1180,13 +1180,15 @@ func makeChatFlowHistoryMessages(ctx context.Context, appID, conversationID, use
return nil, err
}
rID = runRecord.ID
} else if msg.Role == assistantRole && rID == 0 {
continue
} else if msg.Role == assistantRole {
if rID == 0 {
continue
}
} else {
return nil, fmt.Errorf("invalid role type %v", msg.Role)
}
m, err := toConversationMessage(ctx, appID, conversationID, userID, rID, sectionID, ternary.IFElse(msg.Role == userRole, message.MessageTypeQuestion, message.MessageTypeAnswer), msg)
m, err := toConversationMessage(ctx, bizID, conversationID, userID, rID, sectionID, ternary.IFElse(msg.Role == userRole, message.MessageTypeQuestion, message.MessageTypeAnswer), msg)
if err != nil {
return nil, err
}
@ -1274,7 +1276,7 @@ func (w *ApplicationService) OpenAPICreateConversation(ctx context.Context, req
}, nil
}
func toConversationMessage(ctx context.Context, appID, cid, userID, roundID, sectionID int64, messageType message.MessageType, msg *workflow.EnterMessage) (*message.Message, error) {
func toConversationMessage(ctx context.Context, bizID, cid, userID, roundID, sectionID int64, messageType message.MessageType, msg *workflow.EnterMessage) (*message.Message, error) {
type content struct {
Type string `json:"type"`
FileID *string `json:"file_id"`
@ -1284,7 +1286,7 @@ func toConversationMessage(ctx context.Context, appID, cid, userID, roundID, sec
return &message.Message{
Role: schema.User,
ConversationID: cid,
AgentID: appID,
AgentID: bizID,
RunID: roundID,
Content: msg.Content,
ContentType: message.ContentTypeText,
@ -1304,7 +1306,7 @@ func toConversationMessage(ctx context.Context, appID, cid, userID, roundID, sec
Role: schema.User,
MessageType: messageType,
ConversationID: cid,
AgentID: appID,
AgentID: bizID,
UserID: strconv.FormatInt(userID, 10),
RunID: roundID,
ContentType: message.ContentTypeMix,
@ -1432,7 +1434,7 @@ func toSchemaMessage(ctx context.Context, msg *workflow.EnterMessage) (*schema.M
type convertToChatFlowInfo struct {
userMessage *schema.Message
appID int64
bizID int64
conversationID int64
roundID int64
workflowID int64

View File

@ -0,0 +1,604 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package workflow
import (
"context"
"errors"
"strconv"
"testing"
"github.com/cloudwego/eino/schema"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
messageentity "github.com/coze-dev/coze-studio/backend/api/model/crossdomain/message"
"github.com/coze-dev/coze-studio/backend/api/model/workflow"
crossagentrun "github.com/coze-dev/coze-studio/backend/crossdomain/contract/agentrun"
"github.com/coze-dev/coze-studio/backend/crossdomain/contract/agentrun/agentrunmock"
crossupload "github.com/coze-dev/coze-studio/backend/crossdomain/contract/upload"
"github.com/coze-dev/coze-studio/backend/crossdomain/contract/upload/uploadmock"
agententity "github.com/coze-dev/coze-studio/backend/domain/conversation/agentrun/entity"
uploadentity "github.com/coze-dev/coze-studio/backend/domain/upload/entity"
"github.com/coze-dev/coze-studio/backend/domain/upload/service"
)
func TestApplicationService_makeChatFlowUserInput(t *testing.T) {
ctx := context.Background()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockUpload := uploadmock.NewMockUploader(ctrl)
crossupload.SetDefaultSVC(mockUpload)
tests := []struct {
name string
message *workflow.EnterMessage
setupMock func()
expected string
expectErr bool
}{
{
name: "content type text",
message: &workflow.EnterMessage{
ContentType: "text",
Content: "hello",
},
setupMock: func() {},
expected: "hello",
expectErr: false,
},
{
name: "content type object_string with text",
message: &workflow.EnterMessage{
ContentType: "object_string",
Content: `[{"type": "text", "text": "hello world"}]`,
},
setupMock: func() {},
expected: "hello world",
expectErr: false,
},
{
name: "content type object_string with file",
message: &workflow.EnterMessage{
ContentType: "object_string",
Content: `[{"type": "file", "file_id": "123"}]`,
},
setupMock: func() {
mockUpload.EXPECT().GetFile(gomock.Any(), &service.GetFileRequest{ID: 123}).Return(&service.GetFileResponse{
File: &uploadentity.File{Url: "https://example.com/file"},
}, nil)
},
expected: "https://example.com/file",
expectErr: false,
},
{
name: "content type object_string with text and file",
message: &workflow.EnterMessage{
ContentType: "object_string",
Content: `[{"type": "text", "text": "see this file"}, {"type": "file", "file_id": "123"}]`,
},
setupMock: func() {
mockUpload.EXPECT().GetFile(gomock.Any(), &service.GetFileRequest{ID: 123}).Return(&service.GetFileResponse{
File: &uploadentity.File{Url: "https://example.com/file"},
}, nil)
},
expected: "see this file,https://example.com/file",
expectErr: false,
},
{
name: "get file error",
message: &workflow.EnterMessage{
ContentType: "object_string",
Content: `[{"type": "file", "file_id": "123"}]`,
},
setupMock: func() {
mockUpload.EXPECT().GetFile(gomock.Any(), &service.GetFileRequest{ID: 123}).Return(nil, errors.New("get file error"))
},
expectErr: true,
},
{
name: "file not found",
message: &workflow.EnterMessage{
ContentType: "object_string",
Content: `[{"type": "file", "file_id": "123"}]`,
},
setupMock: func() {
mockUpload.EXPECT().GetFile(gomock.Any(), &service.GetFileRequest{ID: 123}).Return(&service.GetFileResponse{
File: nil,
}, nil)
},
expectErr: true,
},
{
name: "invalid content type",
message: &workflow.EnterMessage{
ContentType: "invalid",
},
setupMock: func() {},
expectErr: true,
},
{
name: "invalid json",
message: &workflow.EnterMessage{
ContentType: "object_string",
Content: `invalid-json`,
},
setupMock: func() {},
expectErr: true,
},
}
w := &ApplicationService{}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.setupMock()
result, err := w.makeChatFlowUserInput(ctx, tt.message)
if tt.expectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
}
})
}
}
func Test_toConversationMessage(t *testing.T) {
ctx := context.Background()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockUpload := uploadmock.NewMockUploader(ctrl)
crossupload.SetDefaultSVC(mockUpload)
bizID, cid, userID, roundID, sectionID := int64(2), int64(1), int64(4), int64(3), int64(5)
tests := []struct {
name string
msg *workflow.EnterMessage
messageType messageentity.MessageType
setupMock func()
expected *messageentity.Message
expectErr bool
}{
{
name: "content type text",
msg: &workflow.EnterMessage{
ContentType: "text",
Content: "hello",
},
messageType: messageentity.MessageTypeQuestion,
setupMock: func() {},
expected: &messageentity.Message{
Role: schema.User,
ConversationID: cid,
AgentID: bizID,
RunID: roundID,
Content: "hello",
ContentType: messageentity.ContentTypeText,
MessageType: messageentity.MessageTypeQuestion,
UserID: strconv.FormatInt(userID, 10),
SectionID: sectionID,
},
expectErr: false,
},
{
name: "content type object_string with text",
msg: &workflow.EnterMessage{
ContentType: "object_string",
Content: `[{"type": "text", "text": "hello"}]`,
},
messageType: messageentity.MessageTypeQuestion,
setupMock: func() {},
expected: &messageentity.Message{
Role: schema.User,
MessageType: messageentity.MessageTypeQuestion,
ConversationID: cid,
AgentID: bizID,
UserID: strconv.FormatInt(userID, 10),
RunID: roundID,
ContentType: messageentity.ContentTypeMix,
MultiContent: []*messageentity.InputMetaData{
{Type: messageentity.InputTypeText, Text: "hello"},
},
SectionID: sectionID,
},
expectErr: false,
},
{
name: "content type object_string with file",
msg: &workflow.EnterMessage{
ContentType: "object_string",
Content: `[{"type": "file", "file_id": "123"}]`,
},
messageType: messageentity.MessageTypeQuestion,
setupMock: func() {
mockUpload.EXPECT().GetFile(gomock.Any(), &service.GetFileRequest{ID: 123}).Return(&service.GetFileResponse{
File: &uploadentity.File{Url: "https://example.com/file", TosURI: "tos://uri", Name: "file.txt"},
}, nil)
},
expected: &messageentity.Message{
Role: schema.User,
MessageType: messageentity.MessageTypeQuestion,
ConversationID: cid,
AgentID: bizID,
UserID: strconv.FormatInt(userID, 10),
RunID: roundID,
ContentType: messageentity.ContentTypeMix,
MultiContent: []*messageentity.InputMetaData{
{
Type: "file",
FileData: []*messageentity.FileData{
{Url: "https://example.com/file", URI: "tos://uri", Name: "file.txt"},
},
},
},
SectionID: sectionID,
},
expectErr: false,
},
{
name: "get file error",
msg: &workflow.EnterMessage{
ContentType: "object_string",
Content: `[{"type": "file", "file_id": "123"}]`,
},
setupMock: func() {
mockUpload.EXPECT().GetFile(gomock.Any(), &service.GetFileRequest{ID: 123}).Return(nil, errors.New("get file error"))
},
expectErr: true,
},
{
name: "file not found",
msg: &workflow.EnterMessage{
ContentType: "object_string",
Content: `[{"type": "file", "file_id": "123"}]`,
},
setupMock: func() {
mockUpload.EXPECT().GetFile(gomock.Any(), &service.GetFileRequest{ID: 123}).Return(&service.GetFileResponse{}, nil)
},
expectErr: true,
},
{
name: "invalid content type",
msg: &workflow.EnterMessage{
ContentType: "invalid",
},
setupMock: func() {},
expectErr: true,
},
{
name: "invalid json",
msg: &workflow.EnterMessage{
ContentType: "object_string",
Content: "invalid-json",
},
setupMock: func() {},
expectErr: true,
},
{
name: "invalid input type",
msg: &workflow.EnterMessage{
ContentType: "object_string",
Content: `[{"type": "invalid"}]`,
},
setupMock: func() {},
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.setupMock()
result, err := toConversationMessage(ctx, bizID, cid, userID, roundID, sectionID, tt.messageType, tt.msg)
if tt.expectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
}
})
}
}
func Test_toSchemaMessage(t *testing.T) {
ctx := context.Background()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockUpload := uploadmock.NewMockUploader(ctrl)
crossupload.SetDefaultSVC(mockUpload)
tests := []struct {
name string
msg *workflow.EnterMessage
setupMock func()
expected *schema.Message
expectErr bool
}{
{
name: "content type text",
msg: &workflow.EnterMessage{
ContentType: "text",
Content: "hello",
},
setupMock: func() {},
expected: &schema.Message{
Role: schema.User,
Content: "hello",
},
expectErr: false,
},
{
name: "content type object_string with text",
msg: &workflow.EnterMessage{
ContentType: "object_string",
Content: `[{"type": "text", "text": "hello"}]`,
},
setupMock: func() {},
expected: &schema.Message{
Role: schema.User,
MultiContent: []schema.ChatMessagePart{
{Type: schema.ChatMessagePartTypeText, Text: "hello"},
},
},
expectErr: false,
},
{
name: "content type object_string with image",
msg: &workflow.EnterMessage{
ContentType: "object_string",
Content: `[{"type": "image", "file_id": "123"}]`,
},
setupMock: func() {
mockUpload.EXPECT().GetFile(gomock.Any(), &service.GetFileRequest{ID: 123}).Return(&service.GetFileResponse{
File: &uploadentity.File{Url: "https://example.com/image.png"},
}, nil)
},
expected: &schema.Message{
Role: schema.User,
MultiContent: []schema.ChatMessagePart{
{
Type: schema.ChatMessagePartTypeImageURL,
ImageURL: &schema.ChatMessageImageURL{URL: "https://example.com/image.png"},
},
},
},
expectErr: false,
},
{
name: "content type object_string with various file types",
msg: &workflow.EnterMessage{
ContentType: "object_string",
Content: `[{"type": "file", "file_id": "1"}, {"type": "audio", "file_id": "2"}, {"type": "video", "file_id": "3"}]`,
},
setupMock: func() {
mockUpload.EXPECT().GetFile(gomock.Any(), &service.GetFileRequest{ID: 1}).Return(&service.GetFileResponse{File: &uploadentity.File{Url: "https://example.com/file"}}, nil)
mockUpload.EXPECT().GetFile(gomock.Any(), &service.GetFileRequest{ID: 2}).Return(&service.GetFileResponse{File: &uploadentity.File{Url: "https://example.com/audio"}}, nil)
mockUpload.EXPECT().GetFile(gomock.Any(), &service.GetFileRequest{ID: 3}).Return(&service.GetFileResponse{File: &uploadentity.File{Url: "https://example.com/video"}}, nil)
},
expected: &schema.Message{
Role: schema.User,
MultiContent: []schema.ChatMessagePart{
{Type: schema.ChatMessagePartTypeFileURL, FileURL: &schema.ChatMessageFileURL{URL: "https://example.com/file"}},
{Type: schema.ChatMessagePartTypeAudioURL, AudioURL: &schema.ChatMessageAudioURL{URL: "https://example.com/audio"}},
{Type: schema.ChatMessagePartTypeVideoURL, VideoURL: &schema.ChatMessageVideoURL{URL: "https://example.com/video"}},
},
},
expectErr: false,
},
{
name: "get file error",
msg: &workflow.EnterMessage{
ContentType: "object_string",
Content: `[{"type": "file", "file_id": "123"}]`,
},
setupMock: func() {
mockUpload.EXPECT().GetFile(gomock.Any(), &service.GetFileRequest{ID: 123}).Return(nil, errors.New("get file error"))
},
expectErr: true,
},
{
name: "file not found",
msg: &workflow.EnterMessage{
ContentType: "object_string",
Content: `[{"type": "file", "file_id": "123"}]`,
},
setupMock: func() {
mockUpload.EXPECT().GetFile(gomock.Any(), &service.GetFileRequest{ID: 123}).Return(&service.GetFileResponse{}, nil)
},
expectErr: true,
},
{
name: "invalid content type",
msg: &workflow.EnterMessage{
ContentType: "invalid",
},
setupMock: func() {},
expectErr: true,
},
{
name: "invalid json",
msg: &workflow.EnterMessage{
ContentType: "object_string",
Content: "invalid-json",
},
setupMock: func() {},
expectErr: true,
},
{
name: "invalid input type",
msg: &workflow.EnterMessage{
ContentType: "object_string",
Content: `[{"type": "invalid"}]`,
},
setupMock: func() {},
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.setupMock()
result, err := toSchemaMessage(ctx, tt.msg)
if tt.expectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
}
})
}
}
func Test_makeChatFlowHistoryMessages(t *testing.T) {
ctx := context.Background()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockAgentRun := agentrunmock.NewMockAgentRun(ctrl)
crossagentrun.SetDefaultSVC(mockAgentRun)
mockUpload := uploadmock.NewMockUploader(ctrl)
crossupload.SetDefaultSVC(mockUpload)
bizID, conversationID, userID, sectionID, connectorID := int64(2), int64(1), int64(3), int64(4), int64(5)
tests := []struct {
name string
messages []*workflow.EnterMessage
setupMock func()
expected []*messageentity.Message
expectErr bool
}{
{
name: "empty messages",
messages: []*workflow.EnterMessage{},
setupMock: func() {},
expected: []*messageentity.Message{},
expectErr: false,
},
{
name: "one user message",
messages: []*workflow.EnterMessage{
{Role: "user", ContentType: "text", Content: "hello"},
},
setupMock: func() {
mockAgentRun.EXPECT().Create(gomock.Any(), gomock.Any()).Return(&agententity.RunRecordMeta{ID: 100}, nil).Times(1)
},
expected: []*messageentity.Message{
{
Role: schema.User,
ConversationID: conversationID,
AgentID: bizID,
RunID: 100,
Content: "hello",
ContentType: messageentity.ContentTypeText,
MessageType: messageentity.MessageTypeQuestion,
UserID: strconv.FormatInt(userID, 10),
SectionID: sectionID,
},
},
expectErr: false,
},
{
name: "user and assistant message",
messages: []*workflow.EnterMessage{
{Role: "user", ContentType: "text", Content: "hello"},
{Role: "assistant", ContentType: "text", Content: "hi"},
},
setupMock: func() {
mockAgentRun.EXPECT().Create(gomock.Any(), gomock.Any()).Return(&agententity.RunRecordMeta{ID: 100}, nil).Times(1)
},
expected: []*messageentity.Message{
{
Role: schema.User,
ConversationID: conversationID,
AgentID: bizID,
RunID: 100,
Content: "hello",
ContentType: messageentity.ContentTypeText,
MessageType: messageentity.MessageTypeQuestion,
UserID: strconv.FormatInt(userID, 10),
SectionID: sectionID,
},
{
Role: schema.User,
ConversationID: conversationID,
AgentID: bizID,
RunID: 100,
Content: "hi",
ContentType: messageentity.ContentTypeText,
MessageType: messageentity.MessageTypeAnswer,
UserID: strconv.FormatInt(userID, 10),
SectionID: sectionID,
},
},
expectErr: false,
},
{
name: "only assistant message",
messages: []*workflow.EnterMessage{
{Role: "assistant", ContentType: "text", Content: "hi"},
},
setupMock: func() {},
expected: []*messageentity.Message{},
expectErr: false,
},
{
name: "create run record error",
messages: []*workflow.EnterMessage{
{Role: "user", ContentType: "text", Content: "hello"},
},
setupMock: func() {
mockAgentRun.EXPECT().Create(gomock.Any(), gomock.Any()).Return(nil, errors.New("db error"))
},
expectErr: true,
},
{
name: "invalid role",
messages: []*workflow.EnterMessage{
{Role: "system", ContentType: "text", Content: "hello"},
},
setupMock: func() {},
expectErr: true,
},
{
name: "toConversationMessage error",
messages: []*workflow.EnterMessage{
{Role: "user", ContentType: "invalid", Content: "hello"},
},
setupMock: func() {
mockAgentRun.EXPECT().Create(gomock.Any(), gomock.Any()).Return(&agententity.RunRecordMeta{ID: 100}, nil)
},
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.setupMock()
result, err := makeChatFlowHistoryMessages(ctx, bizID, conversationID, userID, sectionID, connectorID, tt.messages)
if tt.expectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
}
})
}
}