feat: Support for Chat Flow & Agent Support for binding a single chat flow (#765)
Co-authored-by: Yu Yang <72337138+tomasyu985@users.noreply.github.com> Co-authored-by: zengxiaohui <csu.zengxiaohui@gmail.com> Co-authored-by: lijunwen.gigoo <lijunwen.gigoo@bytedance.com> Co-authored-by: lvxinyu.1117 <lvxinyu.1117@bytedance.com> Co-authored-by: liuyunchao.0510 <liuyunchao.0510@bytedance.com> Co-authored-by: haozhenfei <37089575+haozhenfei@users.noreply.github.com> Co-authored-by: July <jiangxujin@bytedance.com> Co-authored-by: tecvan-fe <fanwenjie.fe@bytedance.com>
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* 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 conversation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
|
||||
workflowModel "github.com/coze-dev/coze-studio/backend/api/model/crossdomain/workflow"
|
||||
crossconversation "github.com/coze-dev/coze-studio/backend/crossdomain/contract/conversation"
|
||||
wf "github.com/coze-dev/coze-studio/backend/domain/workflow"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/canvas/convert"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/execute"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/nodes"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/schema"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/errorx"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ternary"
|
||||
"github.com/coze-dev/coze-studio/backend/types/errno"
|
||||
)
|
||||
|
||||
type ClearConversationHistoryConfig struct{}
|
||||
|
||||
type ClearConversationHistory struct{}
|
||||
|
||||
func (c *ClearConversationHistoryConfig) Adapt(_ context.Context, n *vo.Node, _ ...nodes.AdaptOption) (*schema.NodeSchema, error) {
|
||||
ns := &schema.NodeSchema{
|
||||
Key: vo.NodeKey(n.ID),
|
||||
Type: entity.NodeTypeClearConversationHistory,
|
||||
Name: n.Data.Meta.Title,
|
||||
Configs: c,
|
||||
}
|
||||
|
||||
if err := convert.SetInputsForNodeSchema(n, ns); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := convert.SetOutputTypesForNodeSchema(n, ns); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ns, nil
|
||||
}
|
||||
|
||||
func (c *ClearConversationHistoryConfig) Build(_ context.Context, ns *schema.NodeSchema, _ ...schema.BuildOption) (any, error) {
|
||||
return &ClearConversationHistory{}, nil
|
||||
}
|
||||
|
||||
func (c *ClearConversationHistory) Invoke(ctx context.Context, in map[string]any) (map[string]any, error) {
|
||||
|
||||
var (
|
||||
execCtx = execute.GetExeCtx(ctx)
|
||||
env = ternary.IFElse(execCtx.ExeCfg.Mode == workflowModel.ExecuteModeRelease, vo.Online, vo.Draft)
|
||||
appID = execCtx.ExeCfg.AppID
|
||||
agentID = execCtx.ExeCfg.AgentID
|
||||
connectorID = execCtx.ExeCfg.ConnectorID
|
||||
userID = execCtx.ExeCfg.Operator
|
||||
version = execCtx.ExeCfg.Version
|
||||
)
|
||||
|
||||
if agentID != nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodesNotAvailable, fmt.Errorf("in the agent scenario, query conversation list is not available"))
|
||||
}
|
||||
if appID == nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodesNotAvailable, fmt.Errorf("query conversation list node, app id is required"))
|
||||
}
|
||||
|
||||
conversationName, ok := in["conversationName"].(string)
|
||||
if !ok {
|
||||
return nil, vo.WrapError(errno.ErrInvalidParameter, errors.New("conversation name is required"))
|
||||
}
|
||||
|
||||
t, existed, err := wf.GetRepository().GetConversationTemplate(ctx, env, vo.GetConversationTemplatePolicy{
|
||||
AppID: appID,
|
||||
Name: ptr.Of(conversationName),
|
||||
Version: ptr.Of(version),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
var conversationID int64
|
||||
if existed {
|
||||
ret, existed, err := wf.GetRepository().GetStaticConversationByTemplateID(ctx, env, userID, connectorID, t.TemplateID)
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
if existed {
|
||||
conversationID = ret.ConversationID
|
||||
}
|
||||
} else {
|
||||
ret, existed, err := wf.GetRepository().GetDynamicConversationByName(ctx, env, *appID, connectorID, userID, conversationName)
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
if existed {
|
||||
conversationID = ret.ConversationID
|
||||
}
|
||||
}
|
||||
|
||||
if !existed {
|
||||
return map[string]any{
|
||||
"isSuccess": false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
resp, err := crossconversation.DefaultSVC().ClearConversationHistory(ctx, &crossconversation.ClearConversationHistoryReq{
|
||||
ConversationID: conversationID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
if resp == nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodeOperationFail, fmt.Errorf("clear conversation history failed, response is nil"))
|
||||
}
|
||||
if execCtx.ExeCfg.SectionID != nil {
|
||||
atomic.StoreInt64(execCtx.ExeCfg.SectionID, resp.SectionID)
|
||||
}
|
||||
return map[string]any{
|
||||
"isSuccess": true,
|
||||
}, nil
|
||||
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
/*
|
||||
* 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 conversation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
workflowModel "github.com/coze-dev/coze-studio/backend/api/model/crossdomain/workflow"
|
||||
crossconversation "github.com/coze-dev/coze-studio/backend/crossdomain/contract/conversation"
|
||||
crossmessage "github.com/coze-dev/coze-studio/backend/crossdomain/contract/message"
|
||||
|
||||
wf "github.com/coze-dev/coze-studio/backend/domain/workflow"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/canvas/convert"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/execute"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/nodes"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/schema"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/errorx"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ternary"
|
||||
"github.com/coze-dev/coze-studio/backend/types/errno"
|
||||
)
|
||||
|
||||
type ConversationHistoryConfig struct{}
|
||||
|
||||
type ConversationHistory struct{}
|
||||
|
||||
func (ch *ConversationHistoryConfig) Adapt(_ context.Context, n *vo.Node, _ ...nodes.AdaptOption) (*schema.NodeSchema, error) {
|
||||
ns := &schema.NodeSchema{
|
||||
Key: vo.NodeKey(n.ID),
|
||||
Type: entity.NodeTypeConversationHistory,
|
||||
Name: n.Data.Meta.Title,
|
||||
Configs: ch,
|
||||
}
|
||||
|
||||
if err := convert.SetInputsForNodeSchema(n, ns); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := convert.SetOutputTypesForNodeSchema(n, ns); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ns, nil
|
||||
}
|
||||
|
||||
func (ch *ConversationHistoryConfig) Build(_ context.Context, ns *schema.NodeSchema, _ ...schema.BuildOption) (any, error) {
|
||||
return &ConversationHistory{}, nil
|
||||
}
|
||||
|
||||
func (ch *ConversationHistory) Invoke(ctx context.Context, input map[string]any) (map[string]any, error) {
|
||||
var (
|
||||
execCtx = execute.GetExeCtx(ctx)
|
||||
env = ternary.IFElse(execCtx.ExeCfg.Mode == workflowModel.ExecuteModeRelease, vo.Online, vo.Draft)
|
||||
appID = execCtx.ExeCfg.AppID
|
||||
agentID = execCtx.ExeCfg.AgentID
|
||||
connectorID = execCtx.ExeCfg.ConnectorID
|
||||
userID = execCtx.ExeCfg.Operator
|
||||
version = execCtx.ExeCfg.Version
|
||||
initRunID = execCtx.ExeCfg.InitRoundID
|
||||
)
|
||||
if agentID != nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodesNotAvailable, fmt.Errorf("in the agent scenario, query conversation list is not available"))
|
||||
}
|
||||
if appID == nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodesNotAvailable, fmt.Errorf("query conversation list node, app id is required"))
|
||||
}
|
||||
|
||||
conversationName, ok := input["conversationName"].(string)
|
||||
if !ok {
|
||||
return nil, vo.WrapError(errno.ErrInvalidParameter, errors.New("conversation name is required"))
|
||||
}
|
||||
|
||||
rounds, ok := input["rounds"].(int64)
|
||||
if !ok {
|
||||
return nil, vo.WrapError(errno.ErrInvalidParameter, errors.New("rounds is required"))
|
||||
}
|
||||
|
||||
template, existed, err := wf.GetRepository().GetConversationTemplate(ctx, env, vo.GetConversationTemplatePolicy{
|
||||
AppID: appID,
|
||||
Name: ptr.Of(conversationName),
|
||||
Version: ptr.Of(version),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
|
||||
var conversationID int64
|
||||
if existed {
|
||||
var sc *entity.StaticConversation
|
||||
sc, existed, err = wf.GetRepository().GetStaticConversationByTemplateID(ctx, env, userID, connectorID, template.TemplateID)
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
if existed {
|
||||
conversationID = sc.ConversationID
|
||||
}
|
||||
|
||||
} else {
|
||||
var dc *entity.DynamicConversation
|
||||
dc, existed, err = wf.GetRepository().GetDynamicConversationByName(ctx, env, *appID, connectorID, userID, conversationName)
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
if existed {
|
||||
conversationID = dc.ConversationID
|
||||
}
|
||||
}
|
||||
|
||||
if !existed {
|
||||
return nil, vo.WrapError(errno.ErrConversationOfAppNotFound, fmt.Errorf("the conversation name does not exist: '%v'", conversationName))
|
||||
}
|
||||
|
||||
currentConversationID := execCtx.ExeCfg.ConversationID
|
||||
isCurrentConversation := currentConversationID != nil && *currentConversationID == conversationID
|
||||
var sectionID int64
|
||||
if isCurrentConversation {
|
||||
if execCtx.ExeCfg.SectionID == nil {
|
||||
return nil, vo.WrapError(errno.ErrInvalidParameter, errors.New("section id is required"))
|
||||
}
|
||||
sectionID = *execCtx.ExeCfg.SectionID
|
||||
} else {
|
||||
cInfo, err := crossconversation.DefaultSVC().GetByID(ctx, conversationID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sectionID = cInfo.SectionID
|
||||
}
|
||||
|
||||
runIDs, err := crossmessage.DefaultSVC().GetLatestRunIDs(ctx, &crossmessage.GetLatestRunIDsRequest{
|
||||
ConversationID: conversationID,
|
||||
UserID: userID,
|
||||
AppID: *appID,
|
||||
Rounds: rounds,
|
||||
InitRunID: initRunID,
|
||||
SectionID: sectionID,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
|
||||
if len(runIDs) == 0 {
|
||||
return map[string]any{
|
||||
"messageList": []any{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
response, err := crossmessage.DefaultSVC().GetMessagesByRunIDs(ctx, &crossmessage.GetMessagesByRunIDsRequest{
|
||||
ConversationID: conversationID,
|
||||
RunIDs: runIDs,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
|
||||
var messageList []any
|
||||
for _, msg := range response.Messages {
|
||||
content, err := nodes.ConvertMessageToString(ctx, msg)
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
messageList = append(messageList, map[string]any{
|
||||
"role": string(msg.Role),
|
||||
"content": content,
|
||||
})
|
||||
}
|
||||
|
||||
return map[string]any{
|
||||
"messageList": messageList,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
/*
|
||||
* 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 conversation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
workflowModel "github.com/coze-dev/coze-studio/backend/api/model/crossdomain/workflow"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/canvas/convert"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/execute"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/nodes"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/schema"
|
||||
"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/ternary"
|
||||
"github.com/coze-dev/coze-studio/backend/types/errno"
|
||||
)
|
||||
|
||||
type ConversationList struct{}
|
||||
|
||||
type ConversationListConfig struct{}
|
||||
|
||||
func (c *ConversationListConfig) Adapt(_ context.Context, n *vo.Node, _ ...nodes.AdaptOption) (*schema.NodeSchema, error) {
|
||||
ns := &schema.NodeSchema{
|
||||
Key: vo.NodeKey(n.ID),
|
||||
Type: entity.NodeTypeConversationList,
|
||||
Name: n.Data.Meta.Title,
|
||||
Configs: c,
|
||||
}
|
||||
|
||||
if err := convert.SetInputsForNodeSchema(n, ns); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := convert.SetOutputTypesForNodeSchema(n, ns); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ns, nil
|
||||
}
|
||||
|
||||
func (c *ConversationListConfig) Build(_ context.Context, ns *schema.NodeSchema, _ ...schema.BuildOption) (any, error) {
|
||||
return &ConversationList{}, nil
|
||||
}
|
||||
|
||||
type conversationInfo struct {
|
||||
conversationName string
|
||||
conversationId string
|
||||
}
|
||||
|
||||
func (c *ConversationList) Invoke(ctx context.Context, _ map[string]any) (map[string]any, error) {
|
||||
var (
|
||||
execCtx = execute.GetExeCtx(ctx)
|
||||
env = ternary.IFElse(execCtx.ExeCfg.Mode == workflowModel.ExecuteModeRelease, vo.Online, vo.Draft)
|
||||
appID = execCtx.ExeCfg.AppID
|
||||
agentID = execCtx.ExeCfg.AgentID
|
||||
connectorID = execCtx.ExeCfg.ConnectorID
|
||||
userID = execCtx.ExeCfg.Operator
|
||||
version = execCtx.ExeCfg.Version
|
||||
)
|
||||
if agentID != nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodesNotAvailable, fmt.Errorf("in the agent scenario, query conversation list is not available"))
|
||||
}
|
||||
if appID == nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodesNotAvailable, fmt.Errorf("query conversation list node, app id is required"))
|
||||
}
|
||||
|
||||
templates, err := workflow.GetRepository().ListConversationTemplate(ctx, env, &vo.ListConversationTemplatePolicy{
|
||||
AppID: *appID,
|
||||
Version: ptr.Of(version),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
templateIds := make([]int64, 0, len(templates))
|
||||
for _, template := range templates {
|
||||
templateIds = append(templateIds, template.TemplateID)
|
||||
}
|
||||
|
||||
staticConversations, err := workflow.GetRepository().MGetStaticConversation(ctx, env, userID, connectorID, templateIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
templateIDToConvID := slices.ToMap(staticConversations, func(conv *entity.StaticConversation) (int64, int64) {
|
||||
return conv.TemplateID, conv.ConversationID
|
||||
})
|
||||
|
||||
var conversationList []conversationInfo
|
||||
|
||||
for _, template := range templates {
|
||||
convID, ok := templateIDToConvID[template.TemplateID]
|
||||
if !ok {
|
||||
convID = 0
|
||||
}
|
||||
conversationList = append(conversationList, conversationInfo{
|
||||
conversationName: template.Name,
|
||||
conversationId: strconv.FormatInt(convID, 10),
|
||||
})
|
||||
}
|
||||
|
||||
dynamicConversations, err := workflow.GetRepository().ListDynamicConversation(ctx, env, &vo.ListConversationPolicy{
|
||||
ListConversationMeta: vo.ListConversationMeta{
|
||||
APPID: *appID,
|
||||
UserID: userID,
|
||||
ConnectorID: connectorID,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, conv := range dynamicConversations {
|
||||
conversationList = append(conversationList, conversationInfo{
|
||||
conversationName: conv.Name,
|
||||
conversationId: strconv.FormatInt(conv.ConversationID, 10),
|
||||
})
|
||||
}
|
||||
|
||||
resultList := make([]any, len(conversationList))
|
||||
for i, v := range conversationList {
|
||||
resultList[i] = map[string]any{
|
||||
"conversationName": v.conversationName,
|
||||
"conversationId": v.conversationId,
|
||||
}
|
||||
}
|
||||
|
||||
return map[string]any{
|
||||
"conversationList": resultList,
|
||||
}, nil
|
||||
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* 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 conversation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/coze-dev/coze-studio/backend/api/model/conversation/common"
|
||||
|
||||
workflowModel "github.com/coze-dev/coze-studio/backend/api/model/crossdomain/workflow"
|
||||
crossconversation "github.com/coze-dev/coze-studio/backend/crossdomain/contract/conversation"
|
||||
conventity "github.com/coze-dev/coze-studio/backend/domain/conversation/conversation/entity"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/canvas/convert"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/execute"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/nodes"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/schema"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ternary"
|
||||
"github.com/coze-dev/coze-studio/backend/types/errno"
|
||||
)
|
||||
|
||||
type CreateConversationConfig struct{}
|
||||
|
||||
type CreateConversation struct{}
|
||||
|
||||
func (c *CreateConversationConfig) Adapt(_ context.Context, n *vo.Node, _ ...nodes.AdaptOption) (*schema.NodeSchema, error) {
|
||||
ns := &schema.NodeSchema{
|
||||
Key: vo.NodeKey(n.ID),
|
||||
Type: entity.NodeTypeCreateConversation,
|
||||
Name: n.Data.Meta.Title,
|
||||
Configs: c,
|
||||
}
|
||||
|
||||
if err := convert.SetInputsForNodeSchema(n, ns); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := convert.SetOutputTypesForNodeSchema(n, ns); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ns, nil
|
||||
}
|
||||
|
||||
func (c *CreateConversationConfig) Build(_ context.Context, ns *schema.NodeSchema, _ ...schema.BuildOption) (any, error) {
|
||||
return &CreateConversation{}, nil
|
||||
}
|
||||
|
||||
func (c *CreateConversation) Invoke(ctx context.Context, input map[string]any) (map[string]any, error) {
|
||||
|
||||
var (
|
||||
execCtx = execute.GetExeCtx(ctx)
|
||||
env = ternary.IFElse(execCtx.ExeCfg.Mode == workflowModel.ExecuteModeRelease, vo.Online, vo.Draft)
|
||||
appID = execCtx.ExeCfg.AppID
|
||||
agentID = execCtx.ExeCfg.AgentID
|
||||
version = execCtx.ExeCfg.Version
|
||||
connectorID = execCtx.ExeCfg.ConnectorID
|
||||
userID = execCtx.ExeCfg.Operator
|
||||
conversationIDGenerator = workflow.ConversationIDGenerator(func(ctx context.Context, appID int64, userID, connectorID int64) (*conventity.Conversation, error) {
|
||||
return crossconversation.DefaultSVC().CreateConversation(ctx, &conventity.CreateMeta{
|
||||
AgentID: appID,
|
||||
UserID: userID,
|
||||
ConnectorID: connectorID,
|
||||
Scene: common.Scene_SceneWorkflow,
|
||||
})
|
||||
})
|
||||
)
|
||||
if agentID != nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodesNotAvailable, fmt.Errorf("in the agent scenario, create conversation is not available"))
|
||||
}
|
||||
|
||||
if appID == nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodesNotAvailable, errors.New("create conversation node, app id is required"))
|
||||
}
|
||||
|
||||
conversationName, ok := input["conversationName"].(string)
|
||||
if !ok {
|
||||
return nil, vo.WrapError(errno.ErrInvalidParameter, errors.New("conversation name is required"))
|
||||
}
|
||||
|
||||
template, existed, err := workflow.GetRepository().GetConversationTemplate(ctx, env, vo.GetConversationTemplatePolicy{
|
||||
AppID: appID,
|
||||
Name: ptr.Of(conversationName),
|
||||
Version: ptr.Of(version),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if existed {
|
||||
cID, _, existed, err := workflow.GetRepository().GetOrCreateStaticConversation(ctx, env, conversationIDGenerator, &vo.CreateStaticConversation{
|
||||
AppID: ptr.From(appID),
|
||||
TemplateID: template.TemplateID,
|
||||
UserID: userID,
|
||||
ConnectorID: connectorID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]any{
|
||||
"isSuccess": true,
|
||||
"conversationId": cID,
|
||||
"isExisted": existed,
|
||||
}, nil
|
||||
}
|
||||
|
||||
cID, _, existed, err := workflow.GetRepository().GetOrCreateDynamicConversation(ctx, env, conversationIDGenerator, &vo.CreateDynamicConversation{
|
||||
AppID: ptr.From(appID),
|
||||
UserID: userID,
|
||||
ConnectorID: connectorID,
|
||||
Name: conversationName,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return map[string]any{
|
||||
"isSuccess": true,
|
||||
"conversationId": cID,
|
||||
"isExisted": existed,
|
||||
}, nil
|
||||
|
||||
}
|
||||
@@ -0,0 +1,299 @@
|
||||
/*
|
||||
* 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 conversation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/coze-dev/coze-studio/backend/api/model/conversation/common"
|
||||
conventity "github.com/coze-dev/coze-studio/backend/domain/conversation/conversation/entity"
|
||||
|
||||
"strconv"
|
||||
"sync/atomic"
|
||||
|
||||
einoSchema "github.com/cloudwego/eino/schema"
|
||||
model "github.com/coze-dev/coze-studio/backend/api/model/crossdomain/message"
|
||||
workflowModel "github.com/coze-dev/coze-studio/backend/api/model/crossdomain/workflow"
|
||||
crossagentrun "github.com/coze-dev/coze-studio/backend/crossdomain/contract/agentrun"
|
||||
crossconversation "github.com/coze-dev/coze-studio/backend/crossdomain/contract/conversation"
|
||||
crossmessage "github.com/coze-dev/coze-studio/backend/crossdomain/contract/message"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/errorx"
|
||||
|
||||
agententity "github.com/coze-dev/coze-studio/backend/domain/conversation/agentrun/entity"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/canvas/convert"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/execute"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/nodes"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/schema"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ternary"
|
||||
"github.com/coze-dev/coze-studio/backend/types/errno"
|
||||
)
|
||||
|
||||
type CreateMessageConfig struct{}
|
||||
|
||||
type CreateMessage struct{}
|
||||
|
||||
func (c *CreateMessageConfig) Adapt(_ context.Context, n *vo.Node, _ ...nodes.AdaptOption) (*schema.NodeSchema, error) {
|
||||
ns := &schema.NodeSchema{
|
||||
Key: vo.NodeKey(n.ID),
|
||||
Type: entity.NodeTypeCreateMessage,
|
||||
Name: n.Data.Meta.Title,
|
||||
Configs: c,
|
||||
}
|
||||
|
||||
if err := convert.SetInputsForNodeSchema(n, ns); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := convert.SetOutputTypesForNodeSchema(n, ns); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ns, nil
|
||||
}
|
||||
|
||||
func (c *CreateMessageConfig) Build(_ context.Context, ns *schema.NodeSchema, _ ...schema.BuildOption) (any, error) {
|
||||
return &CreateMessage{}, nil
|
||||
}
|
||||
|
||||
func (c *CreateMessage) getConversationIDByName(ctx context.Context, env vo.Env, appID *int64, version, conversationName string, userID, connectorID int64) (int64, error) {
|
||||
template, isExist, err := workflow.GetRepository().GetConversationTemplate(ctx, env, vo.GetConversationTemplatePolicy{
|
||||
AppID: appID,
|
||||
Name: ptr.Of(conversationName),
|
||||
Version: ptr.Of(version),
|
||||
})
|
||||
if err != nil {
|
||||
return 0, vo.WrapError(errno.ErrMessageNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
|
||||
conversationIDGenerator := workflow.ConversationIDGenerator(func(ctx context.Context, appID int64, userID, connectorID int64) (*conventity.Conversation, error) {
|
||||
return crossconversation.DefaultSVC().CreateConversation(ctx, &conventity.CreateMeta{
|
||||
AgentID: appID,
|
||||
UserID: userID,
|
||||
ConnectorID: connectorID,
|
||||
Scene: common.Scene_SceneWorkflow,
|
||||
})
|
||||
})
|
||||
|
||||
var conversationID int64
|
||||
if isExist {
|
||||
cID, _, _, err := workflow.GetRepository().GetOrCreateStaticConversation(ctx, env, conversationIDGenerator, &vo.CreateStaticConversation{
|
||||
AppID: ptr.From(appID),
|
||||
TemplateID: template.TemplateID,
|
||||
UserID: userID,
|
||||
ConnectorID: connectorID,
|
||||
})
|
||||
if err != nil {
|
||||
return 0, vo.WrapError(errno.ErrMessageNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
conversationID = cID
|
||||
} else {
|
||||
dc, _, err := workflow.GetRepository().GetDynamicConversationByName(ctx, env, *appID, connectorID, userID, conversationName)
|
||||
if err != nil {
|
||||
return 0, vo.WrapError(errno.ErrMessageNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
if dc != nil {
|
||||
conversationID = dc.ConversationID
|
||||
}
|
||||
}
|
||||
return conversationID, nil
|
||||
}
|
||||
|
||||
func (c *CreateMessage) Invoke(ctx context.Context, input map[string]any) (map[string]any, error) {
|
||||
var (
|
||||
execCtx = execute.GetExeCtx(ctx)
|
||||
env = ternary.IFElse(execCtx.ExeCfg.Mode == workflowModel.ExecuteModeRelease, vo.Online, vo.Draft)
|
||||
appID = execCtx.ExeCfg.AppID
|
||||
agentID = execCtx.ExeCfg.AgentID
|
||||
version = execCtx.ExeCfg.Version
|
||||
connectorID = execCtx.ExeCfg.ConnectorID
|
||||
userID = execCtx.ExeCfg.Operator
|
||||
)
|
||||
|
||||
conversationName, ok := input["conversationName"].(string)
|
||||
if !ok {
|
||||
return nil, vo.WrapError(errno.ErrInvalidParameter, errors.New("conversationName is required"))
|
||||
}
|
||||
|
||||
role, ok := input["role"].(string)
|
||||
if !ok {
|
||||
return nil, vo.WrapError(errno.ErrInvalidParameter, errors.New("role is required"))
|
||||
}
|
||||
if role != "user" && role != "assistant" {
|
||||
return nil, vo.WrapError(errno.ErrInvalidParameter, fmt.Errorf("role must be user or assistant"))
|
||||
}
|
||||
|
||||
content, ok := input["content"].(string)
|
||||
if !ok {
|
||||
return nil, vo.WrapError(errno.ErrInvalidParameter, errors.New("content is required"))
|
||||
}
|
||||
|
||||
var conversationID int64
|
||||
var err error
|
||||
var resolvedAppID int64
|
||||
if appID == nil {
|
||||
if conversationName != "Default" {
|
||||
return nil, vo.WrapError(errno.ErrOnlyDefaultConversationAllowInAgentScenario, errors.New("conversation node only allow in application"))
|
||||
}
|
||||
if agentID == nil || execCtx.ExeCfg.ConversationID == nil {
|
||||
return map[string]any{
|
||||
"isSuccess": false,
|
||||
"message": map[string]any{
|
||||
"messageId": "0",
|
||||
"role": role,
|
||||
"contentType": "text",
|
||||
"content": content,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
conversationID = *execCtx.ExeCfg.ConversationID
|
||||
resolvedAppID = *agentID
|
||||
} else {
|
||||
conversationID, err = c.getConversationIDByName(ctx, env, appID, version, conversationName, userID, connectorID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resolvedAppID = *appID
|
||||
}
|
||||
|
||||
if conversationID == 0 {
|
||||
return map[string]any{
|
||||
"isSuccess": false,
|
||||
"message": map[string]any{
|
||||
"messageId": "0",
|
||||
"role": role,
|
||||
"contentType": "text",
|
||||
"content": content,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
currentConversationID := execCtx.ExeCfg.ConversationID
|
||||
isCurrentConversation := currentConversationID != nil && *currentConversationID == conversationID
|
||||
var runID int64
|
||||
var sectionID int64
|
||||
if isCurrentConversation {
|
||||
if execCtx.ExeCfg.SectionID != nil {
|
||||
sectionID = *execCtx.ExeCfg.SectionID
|
||||
} else {
|
||||
return nil, vo.WrapError(errno.ErrInvalidParameter, errors.New("section id is required"))
|
||||
}
|
||||
} else {
|
||||
cInfo, err := crossconversation.DefaultSVC().GetByID(ctx, conversationID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sectionID = cInfo.SectionID
|
||||
}
|
||||
|
||||
if role == "user" {
|
||||
// For user messages, always create a new run and store the ID in the context.
|
||||
runRecord, err := crossagentrun.DefaultSVC().Create(ctx, &agententity.AgentRunMeta{
|
||||
AgentID: resolvedAppID,
|
||||
ConversationID: conversationID,
|
||||
UserID: strconv.FormatInt(userID, 10),
|
||||
ConnectorID: connectorID,
|
||||
SectionID: sectionID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
newRunID := runRecord.ID
|
||||
if execCtx.ExeCfg.RoundID != nil {
|
||||
atomic.StoreInt64(execCtx.ExeCfg.RoundID, newRunID)
|
||||
}
|
||||
runID = newRunID
|
||||
} else if isCurrentConversation {
|
||||
// For assistant messages in the same conversation, reuse the runID from the context.
|
||||
if execCtx.ExeCfg.RoundID == nil {
|
||||
// This indicates an inconsistent state, as a user message should have set this.
|
||||
return map[string]any{
|
||||
"isSuccess": false,
|
||||
"message": map[string]any{
|
||||
"messageId": "0",
|
||||
"role": role,
|
||||
"contentType": "text",
|
||||
"content": content,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
runID = *execCtx.ExeCfg.RoundID
|
||||
} else {
|
||||
// For assistant messages in a different conversation or a new workflow run,
|
||||
// find the latest runID or create a new one as a fallback.
|
||||
runIDs, err := crossmessage.DefaultSVC().GetLatestRunIDs(ctx, &crossmessage.GetLatestRunIDsRequest{
|
||||
ConversationID: conversationID,
|
||||
UserID: userID,
|
||||
AppID: resolvedAppID,
|
||||
Rounds: 1,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(runIDs) > 0 && runIDs[0] != 0 {
|
||||
runID = runIDs[0]
|
||||
} else {
|
||||
runRecord, err := crossagentrun.DefaultSVC().Create(ctx, &agententity.AgentRunMeta{
|
||||
AgentID: resolvedAppID,
|
||||
ConversationID: conversationID,
|
||||
UserID: strconv.FormatInt(userID, 10),
|
||||
ConnectorID: connectorID,
|
||||
SectionID: sectionID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrMessageNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
runID = runRecord.ID
|
||||
}
|
||||
}
|
||||
|
||||
message := &model.Message{
|
||||
ConversationID: conversationID,
|
||||
Role: einoSchema.RoleType(role),
|
||||
Content: content,
|
||||
ContentType: model.ContentType("text"),
|
||||
UserID: strconv.FormatInt(userID, 10),
|
||||
AgentID: resolvedAppID,
|
||||
RunID: runID,
|
||||
SectionID: sectionID,
|
||||
}
|
||||
if message.Role == einoSchema.User {
|
||||
message.MessageType = model.MessageTypeQuestion
|
||||
} else {
|
||||
message.MessageType = model.MessageTypeAnswer
|
||||
}
|
||||
msg, err := crossmessage.DefaultSVC().Create(ctx, message)
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrMessageNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
|
||||
messageOutput := map[string]any{
|
||||
"messageId": msg.ID,
|
||||
"role": role,
|
||||
"contentType": "text",
|
||||
"content": content,
|
||||
}
|
||||
|
||||
return map[string]any{
|
||||
"isSuccess": true,
|
||||
"message": messageOutput,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* 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 conversation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
workflowModel "github.com/coze-dev/coze-studio/backend/api/model/crossdomain/workflow"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/canvas/convert"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/execute"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/nodes"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/schema"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ternary"
|
||||
"github.com/coze-dev/coze-studio/backend/types/errno"
|
||||
)
|
||||
|
||||
type DeleteConversationConfig struct{}
|
||||
|
||||
type DeleteConversation struct{}
|
||||
|
||||
func (d *DeleteConversationConfig) Adapt(_ context.Context, n *vo.Node, _ ...nodes.AdaptOption) (*schema.NodeSchema, error) {
|
||||
ns := &schema.NodeSchema{
|
||||
Key: vo.NodeKey(n.ID),
|
||||
Type: entity.NodeTypeConversationDelete,
|
||||
Name: n.Data.Meta.Title,
|
||||
Configs: d,
|
||||
}
|
||||
|
||||
if err := convert.SetInputsForNodeSchema(n, ns); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := convert.SetOutputTypesForNodeSchema(n, ns); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ns, nil
|
||||
}
|
||||
|
||||
func (d *DeleteConversationConfig) Build(_ context.Context, ns *schema.NodeSchema, _ ...schema.BuildOption) (any, error) {
|
||||
return &DeleteConversation{}, nil
|
||||
}
|
||||
|
||||
func (d *DeleteConversation) Invoke(ctx context.Context, in map[string]any) (map[string]any, error) {
|
||||
|
||||
var (
|
||||
execCtx = execute.GetExeCtx(ctx)
|
||||
env = ternary.IFElse(execCtx.ExeCfg.Mode == workflowModel.ExecuteModeRelease, vo.Online, vo.Draft)
|
||||
appID = execCtx.ExeCfg.AppID
|
||||
agentID = execCtx.ExeCfg.AgentID
|
||||
version = execCtx.ExeCfg.Version
|
||||
connectorID = execCtx.ExeCfg.ConnectorID
|
||||
userID = execCtx.ExeCfg.Operator
|
||||
)
|
||||
|
||||
if agentID != nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodesNotAvailable, fmt.Errorf("in the agent scenario, delete conversation is not available"))
|
||||
}
|
||||
|
||||
if appID == nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodesNotAvailable, errors.New("delete conversation node, app id is required"))
|
||||
}
|
||||
|
||||
cName, ok := in["conversationName"]
|
||||
if !ok {
|
||||
return nil, vo.WrapError(errno.ErrInvalidParameter, errors.New("conversation name is required"))
|
||||
}
|
||||
|
||||
conversationName := cName.(string)
|
||||
|
||||
_, existed, err := workflow.GetRepository().GetConversationTemplate(ctx, env, vo.GetConversationTemplatePolicy{
|
||||
AppID: appID,
|
||||
Name: ptr.Of(conversationName),
|
||||
Version: ptr.Of(version),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if existed {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodeInvalidOperation, fmt.Errorf("only conversation created through nodes are allowed to be modified or deleted"))
|
||||
}
|
||||
|
||||
dyConversation, existed, err := workflow.GetRepository().GetDynamicConversationByName(ctx, env, *appID, connectorID, userID, conversationName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !existed {
|
||||
return nil, vo.WrapError(errno.ErrConversationOfAppNotFound, fmt.Errorf("the conversation name does not exist: '%v'", conversationName))
|
||||
}
|
||||
|
||||
_, err = workflow.GetRepository().DeleteDynamicConversation(ctx, env, dyConversation.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return map[string]any{
|
||||
"isSuccess": true,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
/*
|
||||
* 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 conversation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
workflowModel "github.com/coze-dev/coze-studio/backend/api/model/crossdomain/workflow"
|
||||
crossmessage "github.com/coze-dev/coze-studio/backend/crossdomain/contract/message"
|
||||
msgentity "github.com/coze-dev/coze-studio/backend/domain/conversation/message/entity"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/errorx"
|
||||
|
||||
wf "github.com/coze-dev/coze-studio/backend/domain/workflow"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/canvas/convert"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/execute"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/nodes"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/schema"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ternary"
|
||||
"github.com/coze-dev/coze-studio/backend/types/errno"
|
||||
)
|
||||
|
||||
type DeleteMessageConfig struct{}
|
||||
|
||||
type DeleteMessage struct{}
|
||||
|
||||
func (d *DeleteMessageConfig) Adapt(_ context.Context, n *vo.Node, _ ...nodes.AdaptOption) (*schema.NodeSchema, error) {
|
||||
ns := &schema.NodeSchema{
|
||||
Key: vo.NodeKey(n.ID),
|
||||
Type: entity.NodeTypeDeleteMessage,
|
||||
Name: n.Data.Meta.Title,
|
||||
Configs: d,
|
||||
}
|
||||
|
||||
if err := convert.SetInputsForNodeSchema(n, ns); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := convert.SetOutputTypesForNodeSchema(n, ns); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ns, nil
|
||||
}
|
||||
|
||||
func (d *DeleteMessageConfig) Build(_ context.Context, ns *schema.NodeSchema, _ ...schema.BuildOption) (any, error) {
|
||||
return &DeleteMessage{}, nil
|
||||
}
|
||||
|
||||
func (d *DeleteMessage) Invoke(ctx context.Context, input map[string]any) (map[string]any, error) {
|
||||
var (
|
||||
execCtx = execute.GetExeCtx(ctx)
|
||||
env = ternary.IFElse(execCtx.ExeCfg.Mode == workflowModel.ExecuteModeRelease, vo.Online, vo.Draft)
|
||||
appID = execCtx.ExeCfg.AppID
|
||||
agentID = execCtx.ExeCfg.AgentID
|
||||
version = execCtx.ExeCfg.Version
|
||||
connectorID = execCtx.ExeCfg.ConnectorID
|
||||
userID = execCtx.ExeCfg.Operator
|
||||
|
||||
successMap = map[string]any{
|
||||
"isSuccess": true,
|
||||
}
|
||||
failedMap = map[string]any{
|
||||
"isSuccess": false,
|
||||
}
|
||||
)
|
||||
|
||||
conversationName, ok := input["conversationName"].(string)
|
||||
if !ok {
|
||||
return nil, vo.WrapError(errno.ErrInvalidParameter, errors.New("conversationName is required"))
|
||||
}
|
||||
messageStr, ok := input["messageId"].(string)
|
||||
if !ok {
|
||||
return nil, vo.WrapError(errno.ErrInvalidParameter, errors.New("messageId is required"))
|
||||
}
|
||||
messageID, err := strconv.ParseInt(messageStr, 10, 64)
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrInvalidParameter, err)
|
||||
}
|
||||
|
||||
if appID == nil {
|
||||
if conversationName != "Default" {
|
||||
return nil, vo.WrapError(errno.ErrOnlyDefaultConversationAllowInAgentScenario, fmt.Errorf("only default conversation allow in agent scenario"))
|
||||
}
|
||||
|
||||
if agentID == nil || execCtx.ExeCfg.ConversationID == nil {
|
||||
return failedMap, nil
|
||||
}
|
||||
|
||||
err = crossmessage.DefaultSVC().Delete(ctx, &msgentity.DeleteMeta{MessageIDs: []int64{messageID}, ConversationID: execCtx.ExeCfg.ConversationID})
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrMessageNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
|
||||
return successMap, nil
|
||||
}
|
||||
|
||||
t, existed, err := wf.GetRepository().GetConversationTemplate(ctx, env, vo.GetConversationTemplatePolicy{
|
||||
AppID: appID,
|
||||
Name: ptr.Of(conversationName),
|
||||
Version: ptr.Of(version),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrMessageNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
|
||||
if existed {
|
||||
sc, existed, err := wf.GetRepository().GetStaticConversationByTemplateID(ctx, env, userID, connectorID, t.TemplateID)
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrMessageNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
|
||||
if !existed {
|
||||
return failedMap, nil
|
||||
}
|
||||
|
||||
err = crossmessage.DefaultSVC().Delete(ctx, &msgentity.DeleteMeta{MessageIDs: []int64{messageID}, ConversationID: ptr.Of(sc.ConversationID)})
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrMessageNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
|
||||
return successMap, nil
|
||||
|
||||
} else {
|
||||
dc, existed, err := wf.GetRepository().GetDynamicConversationByName(ctx, env, *appID, connectorID, userID, conversationName)
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrMessageNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
|
||||
if !existed {
|
||||
return failedMap, nil
|
||||
}
|
||||
|
||||
err = crossmessage.DefaultSVC().Delete(ctx, &msgentity.DeleteMeta{MessageIDs: []int64{messageID}, ConversationID: ptr.Of(dc.ConversationID)})
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrMessageNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
|
||||
return successMap, nil
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* 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 conversation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
model "github.com/coze-dev/coze-studio/backend/api/model/crossdomain/message"
|
||||
"strconv"
|
||||
|
||||
workflowModel "github.com/coze-dev/coze-studio/backend/api/model/crossdomain/workflow"
|
||||
"github.com/coze-dev/coze-studio/backend/crossdomain/contract/message"
|
||||
crossmessage "github.com/coze-dev/coze-studio/backend/crossdomain/contract/message"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/canvas/convert"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/execute"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/nodes"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/schema"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/errorx"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ternary"
|
||||
"github.com/coze-dev/coze-studio/backend/types/errno"
|
||||
)
|
||||
|
||||
type EditMessageConfig struct{}
|
||||
|
||||
type EditMessage struct{}
|
||||
|
||||
func (e *EditMessageConfig) Adapt(_ context.Context, n *vo.Node, _ ...nodes.AdaptOption) (*schema.NodeSchema, error) {
|
||||
ns := &schema.NodeSchema{
|
||||
Key: vo.NodeKey(n.ID),
|
||||
Type: entity.NodeTypeEditMessage,
|
||||
Name: n.Data.Meta.Title,
|
||||
Configs: e,
|
||||
}
|
||||
|
||||
if err := convert.SetInputsForNodeSchema(n, ns); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := convert.SetOutputTypesForNodeSchema(n, ns); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ns, nil
|
||||
}
|
||||
|
||||
func (e *EditMessageConfig) Build(_ context.Context, ns *schema.NodeSchema, _ ...schema.BuildOption) (any, error) {
|
||||
return &EditMessage{}, nil
|
||||
}
|
||||
|
||||
func (e *EditMessage) Invoke(ctx context.Context, input map[string]any) (map[string]any, error) {
|
||||
var (
|
||||
execCtx = execute.GetExeCtx(ctx)
|
||||
env = ternary.IFElse(execCtx.ExeCfg.Mode == workflowModel.ExecuteModeRelease, vo.Online, vo.Draft)
|
||||
appID = execCtx.ExeCfg.AppID
|
||||
agentID = execCtx.ExeCfg.AgentID
|
||||
version = execCtx.ExeCfg.Version
|
||||
connectorID = execCtx.ExeCfg.ConnectorID
|
||||
userID = execCtx.ExeCfg.Operator
|
||||
|
||||
successMap = map[string]any{
|
||||
"isSuccess": true,
|
||||
}
|
||||
failedMap = map[string]any{
|
||||
"isSuccess": false,
|
||||
}
|
||||
)
|
||||
|
||||
conversationName, ok := input["conversationName"].(string)
|
||||
if !ok {
|
||||
return nil, vo.WrapError(errno.ErrInvalidParameter, errors.New("conversationName is required"))
|
||||
}
|
||||
|
||||
messageStr, ok := input["messageId"].(string)
|
||||
if !ok {
|
||||
return nil, vo.WrapError(errno.ErrInvalidParameter, errors.New("messageId is required"))
|
||||
}
|
||||
|
||||
messageID, err := strconv.ParseInt(messageStr, 10, 64)
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrMessageNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
|
||||
newContent, ok := input["newContent"].(string)
|
||||
if !ok {
|
||||
return nil, vo.WrapError(errno.ErrInvalidParameter, errors.New("newContent is required"))
|
||||
}
|
||||
|
||||
if appID == nil {
|
||||
if conversationName != "Default" {
|
||||
return nil, vo.WrapError(errno.ErrOnlyDefaultConversationAllowInAgentScenario, fmt.Errorf("only default conversation allow in agent scenario"))
|
||||
}
|
||||
|
||||
if agentID == nil || execCtx.ExeCfg.ConversationID == nil {
|
||||
return failedMap, nil
|
||||
}
|
||||
|
||||
_, err = crossmessage.DefaultSVC().Edit(ctx, &model.Message{ConversationID: *execCtx.ExeCfg.ConversationID, ID: messageID, Content: newContent})
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrMessageNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
return successMap, err
|
||||
}
|
||||
|
||||
msg, err := message.DefaultSVC().GetMessageByID(ctx, messageID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if msg == nil {
|
||||
return nil, vo.NewError(errno.ErrMessageNodeOperationFail, errorx.KV("cause", "message not found"))
|
||||
}
|
||||
|
||||
if msg.Content == newContent {
|
||||
return successMap, nil
|
||||
}
|
||||
|
||||
t, existed, err := workflow.GetRepository().GetConversationTemplate(ctx, env, vo.GetConversationTemplatePolicy{
|
||||
AppID: appID,
|
||||
Name: ptr.Of(conversationName),
|
||||
Version: ptr.Of(version),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrMessageNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
|
||||
if existed {
|
||||
sts, existed, err := workflow.GetRepository().GetStaticConversationByTemplateID(ctx, env, userID, connectorID, t.TemplateID)
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrMessageNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
|
||||
if !existed {
|
||||
return failedMap, nil
|
||||
}
|
||||
|
||||
_, err = crossmessage.DefaultSVC().Edit(ctx, &model.Message{ConversationID: sts.ConversationID, ID: messageID, Content: newContent})
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrMessageNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
|
||||
return successMap, nil
|
||||
|
||||
} else {
|
||||
dyConversation, existed, err := workflow.GetRepository().GetDynamicConversationByName(ctx, env, *appID, connectorID, userID, conversationName)
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrMessageNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
|
||||
if !existed {
|
||||
return failedMap, nil
|
||||
}
|
||||
|
||||
_, err = crossmessage.DefaultSVC().Edit(ctx, &model.Message{ConversationID: dyConversation.ConversationID, ID: messageID, Content: newContent})
|
||||
if err != nil {
|
||||
return nil, vo.WrapError(errno.ErrMessageNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
|
||||
return successMap, nil
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
/*
|
||||
* 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 conversation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
workflowModel "github.com/coze-dev/coze-studio/backend/api/model/crossdomain/workflow"
|
||||
crossmessage "github.com/coze-dev/coze-studio/backend/crossdomain/contract/message"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/canvas/convert"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/execute"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/nodes"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/schema"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/errorx"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ternary"
|
||||
"github.com/coze-dev/coze-studio/backend/types/errno"
|
||||
)
|
||||
|
||||
type MessageListConfig struct{}
|
||||
|
||||
type MessageList struct{}
|
||||
|
||||
func (m *MessageListConfig) Adapt(_ context.Context, n *vo.Node, _ ...nodes.AdaptOption) (*schema.NodeSchema, error) {
|
||||
ns := &schema.NodeSchema{
|
||||
Key: vo.NodeKey(n.ID),
|
||||
Type: entity.NodeTypeMessageList,
|
||||
Name: n.Data.Meta.Title,
|
||||
Configs: m,
|
||||
}
|
||||
|
||||
if err := convert.SetInputsForNodeSchema(n, ns); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := convert.SetOutputTypesForNodeSchema(n, ns); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ns, nil
|
||||
}
|
||||
|
||||
func (m *MessageListConfig) Build(_ context.Context, ns *schema.NodeSchema, _ ...schema.BuildOption) (any, error) {
|
||||
return &MessageList{}, nil
|
||||
}
|
||||
|
||||
func (m *MessageList) getConversationIDByName(ctx context.Context, env vo.Env, appID *int64, version, conversationName string, userID, connectorID int64) (int64, error) {
|
||||
template, isExist, err := workflow.GetRepository().GetConversationTemplate(ctx, env, vo.GetConversationTemplatePolicy{
|
||||
AppID: appID,
|
||||
Name: ptr.Of(conversationName),
|
||||
Version: ptr.Of(version),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return 0, vo.WrapError(errno.ErrMessageNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
|
||||
var conversationID int64
|
||||
if isExist {
|
||||
sc, _, err := workflow.GetRepository().GetStaticConversationByTemplateID(ctx, env, userID, connectorID, template.TemplateID)
|
||||
if err != nil {
|
||||
return 0, vo.WrapError(errno.ErrMessageNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
if sc != nil {
|
||||
conversationID = sc.ConversationID
|
||||
}
|
||||
} else {
|
||||
dc, _, err := workflow.GetRepository().GetDynamicConversationByName(ctx, env, *appID, connectorID, userID, conversationName)
|
||||
if err != nil {
|
||||
return 0, vo.WrapError(errno.ErrMessageNodeOperationFail, err, errorx.KV("cause", vo.UnwrapRootErr(err).Error()))
|
||||
}
|
||||
if dc != nil {
|
||||
conversationID = dc.ConversationID
|
||||
}
|
||||
}
|
||||
|
||||
return conversationID, nil
|
||||
}
|
||||
|
||||
func (m *MessageList) Invoke(ctx context.Context, input map[string]any) (map[string]any, error) {
|
||||
var (
|
||||
execCtx = execute.GetExeCtx(ctx)
|
||||
env = ternary.IFElse(execCtx.ExeCfg.Mode == workflowModel.ExecuteModeRelease, vo.Online, vo.Draft)
|
||||
appID = execCtx.ExeCfg.AppID
|
||||
agentID = execCtx.ExeCfg.AgentID
|
||||
version = execCtx.ExeCfg.Version
|
||||
connectorID = execCtx.ExeCfg.ConnectorID
|
||||
userID = execCtx.ExeCfg.Operator
|
||||
)
|
||||
|
||||
conversationName, ok := input["conversationName"].(string)
|
||||
if !ok {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodeInvalidOperation, errors.New("ConversationName is required"))
|
||||
}
|
||||
|
||||
var conversationID int64
|
||||
var err error
|
||||
var resolvedAppID int64
|
||||
if appID == nil {
|
||||
if conversationName != "Default" {
|
||||
return nil, vo.WrapError(errno.ErrOnlyDefaultConversationAllowInAgentScenario, errors.New("conversation node only allow in application"))
|
||||
}
|
||||
if agentID == nil || execCtx.ExeCfg.ConversationID == nil {
|
||||
return map[string]any{
|
||||
"messageList": []any{},
|
||||
"firstId": "0",
|
||||
"lastId": "0",
|
||||
"hasMore": false,
|
||||
}, nil
|
||||
}
|
||||
conversationID = *execCtx.ExeCfg.ConversationID
|
||||
resolvedAppID = *agentID
|
||||
} else {
|
||||
conversationID, err = m.getConversationIDByName(ctx, env, appID, version, conversationName, userID, connectorID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resolvedAppID = *appID
|
||||
}
|
||||
|
||||
req := &crossmessage.MessageListRequest{
|
||||
UserID: userID,
|
||||
AppID: resolvedAppID,
|
||||
ConversationID: conversationID,
|
||||
}
|
||||
|
||||
if req.ConversationID == 0 {
|
||||
return map[string]any{
|
||||
"messageList": []any{},
|
||||
"firstId": "0",
|
||||
"lastId": "0",
|
||||
"hasMore": false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
limit, ok := input["limit"].(int64)
|
||||
if ok {
|
||||
if limit > 0 && limit <= 50 {
|
||||
req.Limit = limit
|
||||
} else {
|
||||
req.Limit = 50
|
||||
}
|
||||
} else {
|
||||
req.Limit = 50
|
||||
}
|
||||
beforeID, ok := input["beforeId"].(string)
|
||||
if ok {
|
||||
|
||||
req.BeforeID = &beforeID
|
||||
}
|
||||
afterID, ok := input["afterId"].(string)
|
||||
if ok {
|
||||
|
||||
req.AfterID = &afterID
|
||||
}
|
||||
|
||||
if beforeID != "" && afterID != "" {
|
||||
return nil, vo.WrapError(errno.ErrInvalidParameter, fmt.Errorf("BeforeID and AfterID cannot be set at the same time"))
|
||||
}
|
||||
|
||||
ml, err := crossmessage.DefaultSVC().MessageList(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var messageList []any
|
||||
for _, msg := range ml.Messages {
|
||||
content, err := nodes.ConvertMessageToString(ctx, msg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
messageList = append(messageList, map[string]any{
|
||||
"messageId": strconv.FormatInt(msg.ID, 10),
|
||||
"role": string(msg.Role),
|
||||
"contentType": msg.ContentType,
|
||||
"content": content,
|
||||
})
|
||||
}
|
||||
|
||||
return map[string]any{
|
||||
"messageList": messageList,
|
||||
"firstId": ml.FirstID,
|
||||
"lastId": ml.LastID,
|
||||
"hasMore": ml.HasMore,
|
||||
}, nil
|
||||
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
/*
|
||||
* 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 conversation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
workflowModel "github.com/coze-dev/coze-studio/backend/api/model/crossdomain/workflow"
|
||||
|
||||
wf "github.com/coze-dev/coze-studio/backend/domain/workflow"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/canvas/convert"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/execute"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/nodes"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/schema"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ternary"
|
||||
"github.com/coze-dev/coze-studio/backend/types/errno"
|
||||
)
|
||||
|
||||
type UpdateConversationConfig struct{}
|
||||
|
||||
type UpdateConversation struct{}
|
||||
|
||||
func (c *UpdateConversationConfig) Adapt(_ context.Context, n *vo.Node, _ ...nodes.AdaptOption) (*schema.NodeSchema, error) {
|
||||
ns := &schema.NodeSchema{
|
||||
Key: vo.NodeKey(n.ID),
|
||||
Type: entity.NodeTypeConversationUpdate,
|
||||
Name: n.Data.Meta.Title,
|
||||
Configs: c,
|
||||
}
|
||||
|
||||
if err := convert.SetInputsForNodeSchema(n, ns); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := convert.SetOutputTypesForNodeSchema(n, ns); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ns, nil
|
||||
}
|
||||
|
||||
func (c *UpdateConversationConfig) Build(_ context.Context, ns *schema.NodeSchema, _ ...schema.BuildOption) (any, error) {
|
||||
return &UpdateConversation{}, nil
|
||||
}
|
||||
|
||||
func (c *UpdateConversation) Invoke(ctx context.Context, in map[string]any) (map[string]any, error) {
|
||||
|
||||
var (
|
||||
execCtx = execute.GetExeCtx(ctx)
|
||||
env = ternary.IFElse(execCtx.ExeCfg.Mode == workflowModel.ExecuteModeRelease, vo.Online, vo.Draft)
|
||||
appID = execCtx.ExeCfg.AppID
|
||||
agentID = execCtx.ExeCfg.AgentID
|
||||
version = execCtx.ExeCfg.Version
|
||||
connectorID = execCtx.ExeCfg.ConnectorID
|
||||
userID = execCtx.ExeCfg.Operator
|
||||
)
|
||||
cName, ok := in["conversationName"]
|
||||
if !ok {
|
||||
return nil, vo.WrapError(errno.ErrInvalidParameter, errors.New("conversation name is required"))
|
||||
}
|
||||
|
||||
conversationName := cName.(string)
|
||||
|
||||
ncName, ok := in["newConversationName"]
|
||||
if !ok {
|
||||
return nil, vo.WrapError(errno.ErrInvalidParameter, errors.New("new conversationName name is required"))
|
||||
}
|
||||
|
||||
newConversationName := ncName.(string)
|
||||
|
||||
if agentID != nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodesNotAvailable, fmt.Errorf("in the agent scenario, update conversation is not available"))
|
||||
}
|
||||
|
||||
if appID == nil {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodesNotAvailable, errors.New("conversation update node, app id is required"))
|
||||
}
|
||||
|
||||
_, existed, err := wf.GetRepository().GetConversationTemplate(ctx, env, vo.GetConversationTemplatePolicy{
|
||||
AppID: appID,
|
||||
Name: ptr.Of(conversationName),
|
||||
Version: ptr.Of(version),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if existed {
|
||||
return nil, vo.WrapError(errno.ErrConversationNodeInvalidOperation, fmt.Errorf("only conversation created through nodes are allowed to be modified or deleted"))
|
||||
}
|
||||
|
||||
conversation, existed, err := wf.GetRepository().GetDynamicConversationByName(ctx, env, *appID, connectorID, userID, conversationName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !existed {
|
||||
return map[string]any{
|
||||
"conversationId": "0",
|
||||
"isSuccess": false,
|
||||
"isExisted": false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
ncConversation, existed, err := wf.GetRepository().GetDynamicConversationByName(ctx, env, *appID, connectorID, userID, newConversationName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if existed {
|
||||
return map[string]any{
|
||||
"conversationId": strconv.FormatInt(ncConversation.ConversationID, 10),
|
||||
"isSuccess": false,
|
||||
"isExisted": true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
err = wf.GetRepository().UpdateDynamicConversationNameByID(ctx, env, conversation.ID, newConversationName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return map[string]any{
|
||||
"conversationId": strconv.FormatInt(conversation.ConversationID, 10),
|
||||
"isSuccess": true,
|
||||
"isExisted": false,
|
||||
}, nil
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user