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:
Zhj
2025-08-28 21:53:32 +08:00
committed by GitHub
parent bbc615a18e
commit d70101c979
503 changed files with 48036 additions and 3427 deletions

View File

@@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"maps"
"strconv"
"strings"
@@ -29,21 +30,26 @@ import (
"github.com/spf13/cast"
model "github.com/coze-dev/coze-studio/backend/api/model/crossdomain/modelmgr"
crossmessage "github.com/coze-dev/coze-studio/backend/crossdomain/contract/message"
crossmodelmgr "github.com/coze-dev/coze-studio/backend/crossdomain/contract/modelmgr"
"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"
schema2 "github.com/coze-dev/coze-studio/backend/domain/workflow/internal/schema"
"github.com/coze-dev/coze-studio/backend/pkg/ctxcache"
"github.com/coze-dev/coze-studio/backend/pkg/lang/ternary"
"github.com/coze-dev/coze-studio/backend/pkg/logs"
"github.com/coze-dev/coze-studio/backend/pkg/sonic"
)
type Config struct {
Intents []string
SystemPrompt string
IsFastMode bool
LLMParams *model.LLMParams
Intents []string
SystemPrompt string
IsFastMode bool
LLMParams *model.LLMParams
ChatHistorySetting *vo.ChatHistorySetting
}
func (c *Config) Adapt(_ context.Context, n *vo.Node, _ ...nodes.AdaptOption) (*schema2.NodeSchema, error) {
@@ -59,6 +65,10 @@ func (c *Config) Adapt(_ context.Context, n *vo.Node, _ ...nodes.AdaptOption) (*
return nil, fmt.Errorf("intent detector node's llmParam is nil")
}
if n.Data.Inputs.ChatHistorySetting != nil {
c.ChatHistorySetting = n.Data.Inputs.ChatHistorySetting
}
llmParam, ok := param.(vo.IntentDetectorLLMParam)
if !ok {
return nil, fmt.Errorf("llm node's llmParam must be LLMParam, got %v", llmParam)
@@ -141,14 +151,16 @@ func (c *Config) Build(ctx context.Context, _ *schema2.NodeSchema, _ ...schema2.
&schema.Message{Content: sptTemplate, Role: schema.System},
&schema.Message{Content: "{{query}}", Role: schema.User})
r, err := chain.AppendChatTemplate(prompts).AppendChatModel(m).Compile(ctx)
r, err := chain.AppendChatTemplate(newHistoryChatTemplate(prompts, c.ChatHistorySetting)).AppendChatModel(m).Compile(ctx)
if err != nil {
return nil, err
}
return &IntentDetector{
isFastMode: c.IsFastMode,
systemPrompt: c.SystemPrompt,
runner: r,
isFastMode: c.IsFastMode,
systemPrompt: c.SystemPrompt,
runner: r,
ChatHistorySetting: c.ChatHistorySetting,
}, nil
}
@@ -182,6 +194,10 @@ func (c *Config) ExpectPorts(ctx context.Context, n *vo.Node) []string {
return expects
}
type contextKey string
const chatHistoryKey contextKey = "chatHistory"
const SystemIntentPrompt = `
# Role
You are an intention classification expert, good at being able to judge which classification the user's input belongs to.
@@ -240,9 +256,10 @@ Note:
const classificationID = "classificationId"
type IntentDetector struct {
isFastMode bool
systemPrompt string
runner compose.Runnable[map[string]any, *schema.Message]
isFastMode bool
systemPrompt string
runner compose.Runnable[map[string]any, *schema.Message]
ChatHistorySetting *vo.ChatHistorySetting
}
func (id *IntentDetector) parseToNodeOut(content string) (map[string]any, error) {
@@ -320,3 +337,66 @@ func toIntentString(its []string) (string, error) {
return sonic.MarshalString(vs)
}
func (id *IntentDetector) ToCallbackInput(ctx context.Context, in map[string]any) (map[string]any, error) {
if id.ChatHistorySetting == nil || !id.ChatHistorySetting.EnableChatHistory {
return in, nil
}
var messages []*crossmessage.WfMessage
var scMessages []*schema.Message
var sectionID *int64
execCtx := execute.GetExeCtx(ctx)
if execCtx != nil {
messages = execCtx.ExeCfg.ConversationHistory
scMessages = execCtx.ExeCfg.ConversationHistorySchemaMessages
sectionID = execCtx.ExeCfg.SectionID
}
ret := map[string]any{
"chatHistory": []any{},
}
maps.Copy(ret, in)
if len(messages) == 0 {
return ret, nil
}
if sectionID != nil && messages[0].SectionID != *sectionID {
return ret, nil
}
maxRounds := int(id.ChatHistorySetting.ChatHistoryRound)
if execCtx != nil && execCtx.ExeCfg.MaxHistoryRounds != nil {
maxRounds = min(int(*execCtx.ExeCfg.MaxHistoryRounds), maxRounds)
}
count := 0
startIdx := 0
for i := len(messages) - 1; i >= 0; i-- {
if messages[i].Role == schema.User {
count++
}
if count >= maxRounds {
startIdx = i
break
}
}
var historyMessages []any
for _, msg := range messages[startIdx:] {
content, err := nodes.ConvertMessageToString(ctx, msg)
if err != nil {
logs.CtxWarnf(ctx, "failed to convert message to string: %v", err)
continue
}
historyMessages = append(historyMessages, map[string]any{
"role": string(msg.Role),
"content": content,
})
}
ctxcache.Store(ctx, chatHistoryKey, scMessages[startIdx:])
ret["chatHistory"] = historyMessages
return ret, nil
}

View File

@@ -0,0 +1,85 @@
/*
* 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 intentdetector
import (
"context"
"fmt"
"github.com/cloudwego/eino/components/prompt"
"github.com/cloudwego/eino/schema"
"github.com/coze-dev/coze-studio/backend/api/model/workflow"
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
"github.com/coze-dev/coze-studio/backend/domain/workflow/internal/execute"
"github.com/coze-dev/coze-studio/backend/pkg/ctxcache"
"github.com/coze-dev/coze-studio/backend/pkg/logs"
)
type historyChatTemplate struct {
basePrompt prompt.ChatTemplate
chatHistorySetting *vo.ChatHistorySetting
}
func newHistoryChatTemplate(basePrompt prompt.ChatTemplate, chatHistorySetting *vo.ChatHistorySetting) prompt.ChatTemplate {
return &historyChatTemplate{
basePrompt: basePrompt,
chatHistorySetting: chatHistorySetting,
}
}
func (t *historyChatTemplate) Format(ctx context.Context, vs map[string]any, opts ...prompt.Option) ([]*schema.Message, error) {
baseMessages, err := t.basePrompt.Format(ctx, vs, opts...)
if err != nil {
return nil, fmt.Errorf("failed to format base prompt: %w", err)
}
if len(baseMessages) == 0 {
return nil, fmt.Errorf("base prompt returned no messages")
}
if t.chatHistorySetting == nil || !t.chatHistorySetting.EnableChatHistory {
return baseMessages, nil
}
exeCtx := execute.GetExeCtx(ctx)
if exeCtx == nil {
logs.CtxWarnf(ctx, "execute context is nil, skipping chat history")
return baseMessages, nil
}
if exeCtx.ExeCfg.WorkflowMode != workflow.WorkflowMode_ChatFlow {
return baseMessages, nil
}
historyMessages, ok := ctxcache.Get[[]*schema.Message](ctx, chatHistoryKey)
if !ok || len(historyMessages) == 0 {
logs.CtxWarnf(ctx, "conversation history is empty")
return baseMessages, nil
}
if len(historyMessages) == 0 {
return baseMessages, nil
}
finalMessages := make([]*schema.Message, 0, len(baseMessages)+len(historyMessages))
finalMessages = append(finalMessages, baseMessages[0]) // System prompt
finalMessages = append(finalMessages, historyMessages...)
if len(baseMessages) > 1 {
finalMessages = append(finalMessages, baseMessages[1:]...) // User prompt and any others
}
return finalMessages, nil
}