feat: manually mirror opencoze's code from bytedance

Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
fanlv
2025-07-20 17:36:12 +08:00
commit 890153324f
14811 changed files with 1923430 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
/*
* 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 singleagent
import (
"context"
"fmt"
"github.com/coze-dev/coze-studio/backend/api/model/ocean/cloud/playground"
)
func makeAgentPopupInfoKey(uid, agentID int64, agentPopupType playground.BotPopupType) string {
return fmt.Sprintf("agent:popup_info:uid:%d:%d:%d", uid, agentID, int64(agentPopupType))
}
func (s *singleAgentImpl) GetAgentPopupCount(ctx context.Context, uid, agentID int64, agentPopupType playground.BotPopupType) (int64, error) {
key := makeAgentPopupInfoKey(uid, agentID, agentPopupType)
count, err := s.CounterRepo.Get(ctx, key)
if err != nil {
return 0, err
}
return count, nil
}
func (s *singleAgentImpl) IncrAgentPopupCount(ctx context.Context, uid, agentID int64, agentPopupType playground.BotPopupType) error {
key := makeAgentPopupInfoKey(uid, agentID, agentPopupType)
return s.CounterRepo.IncrBy(ctx, key, 1)
}

View File

@@ -0,0 +1,148 @@
/*
* 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 singleagent
import (
"context"
"sort"
"time"
"github.com/coze-dev/coze-studio/backend/api/model/ocean/cloud/developer_api"
"github.com/coze-dev/coze-studio/backend/crossdomain/contract/crossconnector"
"github.com/coze-dev/coze-studio/backend/domain/agent/singleagent/entity"
"github.com/coze-dev/coze-studio/backend/pkg/lang/conv"
"github.com/coze-dev/coze-studio/backend/pkg/logs"
"github.com/coze-dev/coze-studio/backend/types/consts"
)
func (s *singleAgentImpl) SavePublishRecord(ctx context.Context, p *entity.SingleAgentPublish, e *entity.SingleAgent) error {
err := s.AgentVersionRepo.SavePublishRecord(ctx, p, e)
if err != nil {
return err
}
err = s.UpdatePublishInfo(ctx, e.AgentID, p.ConnectorIds)
if err != nil {
logs.CtxWarnf(ctx, "update publish info failed: %v, agentID: %d , connectorIDs: %v", err, e.AgentID, p.ConnectorIds)
}
return nil
}
func (s *singleAgentImpl) GetPublishedTime(ctx context.Context, agentID int64) (int64, error) {
pubInfo, err := s.PublishInfoRepo.Get(ctx, conv.Int64ToStr(agentID))
if err != nil {
return 0, err
}
return pubInfo.LastPublishTimeMS, nil
}
func (s *singleAgentImpl) UpdatePublishInfo(ctx context.Context, agentID int64, connectorIDs []int64) error {
now := time.Now().UnixMilli()
pubInfo, err := s.PublishInfoRepo.Get(ctx, conv.Int64ToStr(agentID))
if err != nil {
return err
}
if pubInfo.LastPublishTimeMS > now {
return nil
}
// Warn: Concurrent publishing may have the risk of overwriting, temporarily ignored.
// Save publish info
pubInfo.LastPublishTimeMS = now
pubInfo.AgentID = agentID
if pubInfo.ConnectorID2PublishTime == nil {
pubInfo.ConnectorID2PublishTime = make(map[int64]int64)
}
for _, connectorID := range connectorIDs {
pubInfo.ConnectorID2PublishTime[connectorID] = now
}
err = s.PublishInfoRepo.Save(ctx, conv.Int64ToStr(agentID), pubInfo)
return err
}
func (s *singleAgentImpl) GetPublishedInfo(ctx context.Context, agentID int64) (*entity.PublishInfo, error) {
return s.PublishInfoRepo.Get(ctx, conv.Int64ToStr(agentID))
}
func (s *singleAgentImpl) GetPublishConnectorList(ctx context.Context, agentID int64) (*entity.PublishConnectorData, error) {
ids := make([]int64, 0, len(entity.PublishConnectorIDWhiteList))
for v := range entity.PublishConnectorIDWhiteList {
ids = append(ids, v)
}
connectorBasicInfos, err := crossconnector.DefaultSVC().GetByIDs(ctx, ids)
if err != nil {
return nil, err
}
pubInfo, err := s.GetPublishedInfo(ctx, agentID)
if err != nil {
return nil, err
}
publishConnectorList := make([]*developer_api.PublishConnectorInfo, 0)
for _, v := range connectorBasicInfos {
publishTime, _ := pubInfo.ConnectorID2PublishTime[v.ID]
isLastPublished := pubInfo.LastPublishTimeMS == publishTime
c := &developer_api.PublishConnectorInfo{
ID: conv.Int64ToStr(v.ID),
Name: v.Name,
Icon: v.URL,
Desc: v.Desc,
ShareLink: "",
ConnectorStatus: developer_api.BotConnectorStatusPtr(developer_api.BotConnectorStatus_Normal),
IsLastPublished: &isLastPublished,
LastPublishTime: publishTime / 1000,
ConfigStatus: developer_api.ConfigStatus_Configured,
AllowPunish: developer_api.AllowPublishStatusPtr(developer_api.AllowPublishStatus_Allowed),
}
// If there are new ones, use a map to maintain the ID to BindType relationship.
if v.ID == consts.WebSDKConnectorID {
c.BindType = developer_api.BindType_WebSDKBind
} else if v.ID == consts.APIConnectorID {
c.BindType = developer_api.BindType_ApiBind
// c.BindInfo = map[string]string{
// "sdk_version": "1.2.0-beta.6", // TODO@fanlv: 确认版本在哪读取?
// }
c.AuthLoginInfo = &developer_api.AuthLoginInfo{}
}
publishConnectorList = append(publishConnectorList, c)
}
sort.Slice(publishConnectorList, func(i, j int) bool {
return publishConnectorList[i].ID < publishConnectorList[j].ID
})
return &entity.PublishConnectorData{
PublishConnectorList: publishConnectorList,
}, nil
}
func (s *singleAgentImpl) CreateSingleAgent(ctx context.Context, connectorID int64, version string, e *entity.SingleAgent) (int64, error) {
return s.AgentVersionRepo.Create(ctx, connectorID, version, e)
}

View File

@@ -0,0 +1,56 @@
/*
* 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 singleagent
import (
"context"
"github.com/cloudwego/eino/schema"
"github.com/coze-dev/coze-studio/backend/api/model/ocean/cloud/playground"
"github.com/coze-dev/coze-studio/backend/domain/agent/singleagent/entity"
)
type SingleAgent interface {
// draft agent
CreateSingleAgentDraft(ctx context.Context, creatorID int64, draft *entity.SingleAgent) (agentID int64, err error)
CreateSingleAgentDraftWithID(ctx context.Context, creatorID, agentID int64, draft *entity.SingleAgent) (int64, error)
MGetSingleAgentDraft(ctx context.Context, agentIDs []int64) (agents []*entity.SingleAgent, err error)
GetSingleAgentDraft(ctx context.Context, agentID int64) (agentInfo *entity.SingleAgent, err error)
UpdateSingleAgentDraft(ctx context.Context, agentInfo *entity.SingleAgent) (err error)
DeleteAgentDraft(ctx context.Context, spaceID, agentID int64) (err error)
UpdateAgentDraftDisplayInfo(ctx context.Context, userID int64, e *entity.AgentDraftDisplayInfo) error
GetAgentDraftDisplayInfo(ctx context.Context, userID, agentID int64) (*entity.AgentDraftDisplayInfo, error)
// online agent
CreateSingleAgent(ctx context.Context, connectorID int64, version string, e *entity.SingleAgent) (int64, error)
DuplicateInMemory(ctx context.Context, req *entity.DuplicateInfo) (newAgent *entity.SingleAgent, err error)
StreamExecute(ctx context.Context, req *entity.ExecuteRequest) (events *schema.StreamReader[*entity.AgentEvent], err error)
GetSingleAgent(ctx context.Context, agentID int64, version string) (botInfo *entity.SingleAgent, err error)
ListAgentPublishHistory(ctx context.Context, agentID int64, pageIndex, pageSize int32, connectorID *int64) ([]*entity.SingleAgentPublish, error)
// ObtainAgentByIdentity support obtain agent by connectorID and agentID
ObtainAgentByIdentity(ctx context.Context, identity *entity.AgentIdentity) (*entity.SingleAgent, error)
GetAgentPopupCount(ctx context.Context, uid, agentID int64, agentPopupType playground.BotPopupType) (int64, error)
IncrAgentPopupCount(ctx context.Context, uid, agentID int64, agentPopupType playground.BotPopupType) error
// Publish
GetPublishedTime(ctx context.Context, agentID int64) (int64, error)
GetPublishedInfo(ctx context.Context, agentID int64) (*entity.PublishInfo, error)
SavePublishRecord(ctx context.Context, p *entity.SingleAgentPublish, e *entity.SingleAgent) error
GetPublishConnectorList(ctx context.Context, agentID int64) (*entity.PublishConnectorData, error)
}

View File

@@ -0,0 +1,326 @@
/*
* 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 singleagent
import (
"context"
"fmt"
"math/rand"
"github.com/cloudwego/eino/compose"
"github.com/jinzhu/copier"
"github.com/cloudwego/eino/schema"
"github.com/coze-dev/coze-studio/backend/api/model/ocean/cloud/bot_common"
"github.com/coze-dev/coze-studio/backend/crossdomain/contract/crossplugin"
"github.com/coze-dev/coze-studio/backend/domain/agent/singleagent/entity"
"github.com/coze-dev/coze-studio/backend/domain/agent/singleagent/internal/agentflow"
"github.com/coze-dev/coze-studio/backend/domain/agent/singleagent/repository"
"github.com/coze-dev/coze-studio/backend/infra/contract/chatmodel"
"github.com/coze-dev/coze-studio/backend/pkg/errorx"
"github.com/coze-dev/coze-studio/backend/pkg/jsoncache"
"github.com/coze-dev/coze-studio/backend/pkg/lang/slices"
"github.com/coze-dev/coze-studio/backend/pkg/logs"
"github.com/coze-dev/coze-studio/backend/types/errno"
)
type singleAgentImpl struct {
Components
}
type Components struct {
ModelFactory chatmodel.Factory
AgentDraftRepo repository.SingleAgentDraftRepo
AgentVersionRepo repository.SingleAgentVersionRepo
PublishInfoRepo *jsoncache.JsonCache[entity.PublishInfo]
CounterRepo repository.CounterRepository
CPStore compose.CheckPointStore
}
func NewService(c *Components) SingleAgent {
s := &singleAgentImpl{
Components: *c,
}
return s
}
func (s *singleAgentImpl) DeleteAgentDraft(ctx context.Context, spaceID, agentID int64) (err error) {
return s.AgentDraftRepo.Delete(ctx, spaceID, agentID)
}
func (s *singleAgentImpl) DuplicateInMemory(ctx context.Context, req *entity.DuplicateInfo) (newAgent *entity.SingleAgent, err error) {
srcAgent := req.DraftAgent
if srcAgent == nil {
return nil, errorx.New(errno.ErrAgentInvalidParamCode,
errorx.KVf("msg", "srcAgent is nil"))
}
newAgent = &entity.SingleAgent{}
err = copier.CopyWithOption(newAgent, srcAgent, copier.Option{DeepCopy: true, IgnoreEmpty: true})
if err != nil {
return nil, err
}
copySuffixNum := rand.Intn(1000)
newAgent.Name = fmt.Sprintf("%v%03d", srcAgent.Name, copySuffixNum)
newAgent.SpaceID = req.SpaceID
newAgent.CreatorID = req.UserID
newAgent.AgentID = req.NewAgentID
return newAgent, nil
}
func (s *singleAgentImpl) MGetSingleAgentDraft(ctx context.Context, agentIDs []int64) (agents []*entity.SingleAgent, err error) {
return s.AgentDraftRepo.MGet(ctx, agentIDs)
}
func (s *singleAgentImpl) StreamExecute(ctx context.Context, req *entity.ExecuteRequest) (events *schema.StreamReader[*entity.AgentEvent], err error) {
ae, err := s.ObtainAgentByIdentity(ctx, req.Identity)
if err != nil {
return nil, err
}
if req.Identity.Version == "" {
req.Identity.Version = ae.Version
}
conf := &agentflow.Config{
Agent: ae,
UserID: req.UserID,
Identity: req.Identity,
ModelFactory: s.ModelFactory,
CPStore: s.CPStore,
}
rn, err := agentflow.BuildAgent(ctx, conf)
if err != nil {
return nil, err
}
exeReq := &agentflow.AgentRequest{
UserID: req.UserID,
Input: req.Input,
History: req.History,
Identity: req.Identity,
ResumeInfo: req.ResumeInfo,
PreCallTools: req.PreCallTools,
}
return rn.StreamExecute(ctx, rn.PreHandlerReq(ctx, exeReq))
}
func (s *singleAgentImpl) GetSingleAgent(ctx context.Context, agentID int64, version string) (botInfo *entity.SingleAgent, err error) {
if len(version) == 0 {
return s.GetSingleAgentDraft(ctx, agentID)
}
agentInfo, err := s.AgentVersionRepo.Get(ctx, agentID, version)
if err != nil {
return nil, err
}
return agentInfo, nil
}
func (s *singleAgentImpl) UpdateSingleAgentDraft(ctx context.Context, agentInfo *entity.SingleAgent) (err error) {
if agentInfo.Plugin != nil {
toolIDs := slices.Transform(agentInfo.Plugin, func(item *bot_common.PluginInfo) int64 {
return item.GetApiId()
})
err = crossplugin.DefaultSVC().BindAgentTools(ctx, agentInfo.AgentID, toolIDs)
if err != nil {
return fmt.Errorf("bind agent tools failed, err=%v", err)
}
}
return s.AgentDraftRepo.Update(ctx, agentInfo)
}
func (s *singleAgentImpl) CreateSingleAgentDraftWithID(ctx context.Context, creatorID, agentID int64, draft *entity.SingleAgent) (int64, error) {
return s.AgentDraftRepo.CreateWithID(ctx, creatorID, agentID, draft)
}
func (s *singleAgentImpl) CreateSingleAgentDraft(ctx context.Context, creatorID int64, draft *entity.SingleAgent) (agentID int64, err error) {
return s.AgentDraftRepo.Create(ctx, creatorID, draft)
}
func (s *singleAgentImpl) GetSingleAgentDraft(ctx context.Context, agentID int64) (*entity.SingleAgent, error) {
return s.AgentDraftRepo.Get(ctx, agentID)
}
func (s *singleAgentImpl) ObtainAgentByIdentity(ctx context.Context, identity *entity.AgentIdentity) (*entity.SingleAgent, error) {
if identity.IsDraft {
return s.GetSingleAgentDraft(ctx, identity.AgentID)
}
agentID := identity.AgentID
connectorID := identity.ConnectorID
version := identity.Version
if connectorID == 0 {
return s.GetSingleAgent(ctx, identity.AgentID, identity.Version)
}
if version == "" {
singleAgentPublish, err := s.ListAgentPublishHistory(ctx, agentID, 1, 1, &connectorID)
if err != nil {
return nil, err
}
if len(singleAgentPublish) == 0 {
return nil, errorx.New(errno.ErrAgentInvalidParamCode,
errorx.KVf("msg", "agent not published, agentID=%d connectorID=%d", agentID, connectorID))
}
version = singleAgentPublish[0].Version
}
return s.AgentVersionRepo.Get(ctx, agentID, version)
}
func (s *singleAgentImpl) UpdateAgentDraftDisplayInfo(ctx context.Context, userID int64, e *entity.AgentDraftDisplayInfo) error {
do, err := s.AgentDraftRepo.GetDisplayInfo(ctx, userID, e.AgentID)
if err != nil {
return err
}
do.SpaceID = e.SpaceID
if e.DisplayInfo != nil && e.DisplayInfo.TabDisplayInfo != nil {
if e.DisplayInfo.TabDisplayInfo.PluginTabStatus != nil {
do.DisplayInfo.TabDisplayInfo.PluginTabStatus = e.DisplayInfo.TabDisplayInfo.PluginTabStatus
}
if e.DisplayInfo.TabDisplayInfo.WorkflowTabStatus != nil {
do.DisplayInfo.TabDisplayInfo.WorkflowTabStatus = e.DisplayInfo.TabDisplayInfo.WorkflowTabStatus
}
if e.DisplayInfo.TabDisplayInfo.KnowledgeTabStatus != nil {
do.DisplayInfo.TabDisplayInfo.KnowledgeTabStatus = e.DisplayInfo.TabDisplayInfo.KnowledgeTabStatus
}
if e.DisplayInfo.TabDisplayInfo.DatabaseTabStatus != nil {
do.DisplayInfo.TabDisplayInfo.DatabaseTabStatus = e.DisplayInfo.TabDisplayInfo.DatabaseTabStatus
}
if e.DisplayInfo.TabDisplayInfo.VariableTabStatus != nil {
do.DisplayInfo.TabDisplayInfo.VariableTabStatus = e.DisplayInfo.TabDisplayInfo.VariableTabStatus
}
if e.DisplayInfo.TabDisplayInfo.OpeningDialogTabStatus != nil {
do.DisplayInfo.TabDisplayInfo.OpeningDialogTabStatus = e.DisplayInfo.TabDisplayInfo.OpeningDialogTabStatus
}
if e.DisplayInfo.TabDisplayInfo.ScheduledTaskTabStatus != nil {
do.DisplayInfo.TabDisplayInfo.ScheduledTaskTabStatus = e.DisplayInfo.TabDisplayInfo.ScheduledTaskTabStatus
}
if e.DisplayInfo.TabDisplayInfo.SuggestionTabStatus != nil {
do.DisplayInfo.TabDisplayInfo.SuggestionTabStatus = e.DisplayInfo.TabDisplayInfo.SuggestionTabStatus
}
if e.DisplayInfo.TabDisplayInfo.TtsTabStatus != nil {
do.DisplayInfo.TabDisplayInfo.TtsTabStatus = e.DisplayInfo.TabDisplayInfo.TtsTabStatus
}
if e.DisplayInfo.TabDisplayInfo.FileboxTabStatus != nil {
do.DisplayInfo.TabDisplayInfo.FileboxTabStatus = e.DisplayInfo.TabDisplayInfo.FileboxTabStatus
}
if e.DisplayInfo.TabDisplayInfo.LongTermMemoryTabStatus != nil {
do.DisplayInfo.TabDisplayInfo.LongTermMemoryTabStatus = e.DisplayInfo.TabDisplayInfo.LongTermMemoryTabStatus
}
if e.DisplayInfo.TabDisplayInfo.AnswerActionTabStatus != nil {
do.DisplayInfo.TabDisplayInfo.AnswerActionTabStatus = e.DisplayInfo.TabDisplayInfo.AnswerActionTabStatus
}
if e.DisplayInfo.TabDisplayInfo.ImageflowTabStatus != nil {
do.DisplayInfo.TabDisplayInfo.ImageflowTabStatus = e.DisplayInfo.TabDisplayInfo.ImageflowTabStatus
}
if e.DisplayInfo.TabDisplayInfo.BackgroundImageTabStatus != nil {
do.DisplayInfo.TabDisplayInfo.BackgroundImageTabStatus = e.DisplayInfo.TabDisplayInfo.BackgroundImageTabStatus
}
if e.DisplayInfo.TabDisplayInfo.ShortcutTabStatus != nil {
do.DisplayInfo.TabDisplayInfo.ShortcutTabStatus = e.DisplayInfo.TabDisplayInfo.ShortcutTabStatus
}
if e.DisplayInfo.TabDisplayInfo.KnowledgeTableTabStatus != nil {
do.DisplayInfo.TabDisplayInfo.KnowledgeTableTabStatus = e.DisplayInfo.TabDisplayInfo.KnowledgeTableTabStatus
}
if e.DisplayInfo.TabDisplayInfo.KnowledgeTextTabStatus != nil {
do.DisplayInfo.TabDisplayInfo.KnowledgeTextTabStatus = e.DisplayInfo.TabDisplayInfo.KnowledgeTextTabStatus
}
if e.DisplayInfo.TabDisplayInfo.KnowledgePhotoTabStatus != nil {
do.DisplayInfo.TabDisplayInfo.KnowledgePhotoTabStatus = e.DisplayInfo.TabDisplayInfo.KnowledgePhotoTabStatus
}
if e.DisplayInfo.TabDisplayInfo.HookInfoTabStatus != nil {
do.DisplayInfo.TabDisplayInfo.HookInfoTabStatus = e.DisplayInfo.TabDisplayInfo.HookInfoTabStatus
}
if e.DisplayInfo.TabDisplayInfo.DefaultUserInputTabStatus != nil {
do.DisplayInfo.TabDisplayInfo.DefaultUserInputTabStatus = e.DisplayInfo.TabDisplayInfo.DefaultUserInputTabStatus
}
}
return s.AgentDraftRepo.UpdateDisplayInfo(ctx, userID, do)
}
func (s *singleAgentImpl) GetAgentDraftDisplayInfo(ctx context.Context, userID, agentID int64) (*entity.AgentDraftDisplayInfo, error) {
return s.AgentDraftRepo.GetDisplayInfo(ctx, userID, agentID)
}
func (s *singleAgentImpl) ListAgentPublishHistory(ctx context.Context, agentID int64, pageIndex, pageSize int32, connectorID *int64) ([]*entity.SingleAgentPublish, error) {
if connectorID == nil {
return s.AgentVersionRepo.List(ctx, agentID, pageIndex, pageSize)
}
logs.CtxInfof(ctx, "ListAgentPublishHistory, agentID=%v, pageIndex=%v, pageSize=%v, connectorID=%v",
agentID, pageIndex, pageSize, *connectorID)
var (
allResults []*entity.SingleAgentPublish
currentPage int32 = 1
maxCount = pageSize * pageIndex
)
// 全量拉取符合条件的记录
for {
pageData, err := s.AgentVersionRepo.List(ctx, agentID, currentPage, 50)
if err != nil {
return nil, err
}
if len(pageData) == 0 {
break
}
// 过滤当前页数据
for _, item := range pageData {
for _, cID := range item.ConnectorIds {
if cID == *connectorID {
allResults = append(allResults, item)
break
}
}
}
if len(allResults) > int(maxCount) {
break
}
currentPage++
}
start := (pageIndex - 1) * pageSize
if start >= int32(len(allResults)) {
return []*entity.SingleAgentPublish{}, nil
}
end := start + pageSize
if end > int32(len(allResults)) {
end = int32(len(allResults))
}
return allResults[start:end], nil
}