diff --git a/backend/api/middleware/log.go b/backend/api/middleware/log.go index 7a23f783f..b581d4d5c 100644 --- a/backend/api/middleware/log.go +++ b/backend/api/middleware/log.go @@ -19,6 +19,7 @@ package middleware import ( "context" "fmt" + "github.com/coze-dev/coze-studio/backend/types/consts" "net/http" "path/filepath" "strings" @@ -84,7 +85,7 @@ func AccessLogMW() app.HandlerFunc { func SetLogIDMW() app.HandlerFunc { return func(ctx context.Context, c *app.RequestContext) { logID := uuid.New().String() - ctx = context.WithValue(ctx, "log-id", logID) + ctx = context.WithValue(ctx, consts.CtxLogIDKey, logID) c.Header("X-Log-ID", logID) c.Next(ctx) diff --git a/backend/api/model/crossdomain/plugin/option.go b/backend/api/model/crossdomain/plugin/option.go index 0842beadd..abdb8ce8f 100644 --- a/backend/api/model/crossdomain/plugin/option.go +++ b/backend/api/model/crossdomain/plugin/option.go @@ -24,6 +24,9 @@ type ExecuteToolOption struct { ToolVersion string Operation *Openapi3Operation InvalidRespProcessStrategy InvalidResponseProcessStrategy + + AgentID int64 + ConversationID int64 } type ExecuteToolOpt func(o *ExecuteToolOption) @@ -65,3 +68,10 @@ func WithAutoGenRespSchema() ExecuteToolOpt { o.AutoGenRespSchema = true } } + +func WithPluginHTTPHeader(agentID, conversationID int64) ExecuteToolOpt { + return func(o *ExecuteToolOption) { + o.AgentID = agentID + o.ConversationID = conversationID + } +} diff --git a/backend/api/model/crossdomain/singleagent/single_agent.go b/backend/api/model/crossdomain/singleagent/single_agent.go index a20c32d57..fe754593e 100644 --- a/backend/api/model/crossdomain/singleagent/single_agent.go +++ b/backend/api/model/crossdomain/singleagent/single_agent.go @@ -112,6 +112,8 @@ type ExecuteRequest struct { History []*schema.Message ResumeInfo *InterruptInfo PreCallTools []*agentrun.ToolsRetriever + + ConversationID int64 } type AgentIdentity struct { diff --git a/backend/crossdomain/contract/agent/single_agent.go b/backend/crossdomain/contract/agent/single_agent.go index 6ce8d116c..d1396bdfe 100644 --- a/backend/crossdomain/contract/agent/single_agent.go +++ b/backend/crossdomain/contract/agent/single_agent.go @@ -37,6 +37,7 @@ type AgentRuntime struct { AgentVersion string UserID string AgentID int64 + ConversationId int64 IsDraft bool SpaceID int64 ConnectorID int64 diff --git a/backend/crossdomain/impl/singleagent/single_agent.go b/backend/crossdomain/impl/singleagent/single_agent.go index 2cc3f522a..6b749413c 100644 --- a/backend/crossdomain/impl/singleagent/single_agent.go +++ b/backend/crossdomain/impl/singleagent/single_agent.go @@ -80,6 +80,8 @@ func (c *impl) buildSingleAgentStreamExecuteReq(ctx context.Context, agentRuntim } }), ResumeInfo: agentRuntime.ResumeInfo, + + ConversationID: agentRuntime.ConversationId, } } diff --git a/backend/domain/agent/singleagent/internal/agentflow/agent_flow_builder.go b/backend/domain/agent/singleagent/internal/agentflow/agent_flow_builder.go index 6a78d3e24..889f20dcd 100644 --- a/backend/domain/agent/singleagent/internal/agentflow/agent_flow_builder.go +++ b/backend/domain/agent/singleagent/internal/agentflow/agent_flow_builder.go @@ -42,6 +42,8 @@ type Config struct { ModelMgr modelmgr.Manager ModelFactory chatmodel.Factory CPStore compose.CheckPointStore + + ConversationID int64 } const ( @@ -108,6 +110,8 @@ func BuildAgent(ctx context.Context, conf *Config) (r *AgentRunner, err error) { userID: conf.UserID, agentIdentity: conf.Identity, toolConf: conf.Agent.Plugin, + + conversationID: conf.ConversationID, }) if err != nil { return nil, err diff --git a/backend/domain/agent/singleagent/internal/agentflow/node_tool_plugin.go b/backend/domain/agent/singleagent/internal/agentflow/node_tool_plugin.go index 8dd2da3bb..753571348 100644 --- a/backend/domain/agent/singleagent/internal/agentflow/node_tool_plugin.go +++ b/backend/domain/agent/singleagent/internal/agentflow/node_tool_plugin.go @@ -38,6 +38,8 @@ type toolConfig struct { userID string agentIdentity *entity.AgentIdentity toolConf []*bot_common.PluginInfo + + conversationID int64 } func newPluginTools(ctx context.Context, conf *toolConfig) ([]tool.InvokableTool, error) { @@ -71,6 +73,9 @@ func newPluginTools(ctx context.Context, conf *toolConfig) ([]tool.InvokableTool isDraft: conf.agentIdentity.IsDraft, projectInfo: projectInfo, toolInfo: ti, + + agentID: conf.agentIdentity.AgentID, + conversationID: conf.conversationID, }) } @@ -82,6 +87,9 @@ type pluginInvokableTool struct { isDraft bool toolInfo *pluginEntity.ToolInfo projectInfo *plugin.ProjectInfo + + agentID int64 + conversationID int64 } func (p *pluginInvokableTool) Info(ctx context.Context) (*schema.ToolInfo, error) { @@ -124,6 +132,7 @@ func (p *pluginInvokableTool) InvokableRun(ctx context.Context, argumentsInJSON plugin.WithInvalidRespProcessStrategy(plugin.InvalidResponseProcessStrategyOfReturnDefault), plugin.WithToolVersion(p.toolInfo.GetVersion()), plugin.WithProjectInfo(p.projectInfo), + plugin.WithPluginHTTPHeader(p.agentID, p.conversationID), } resp, err := crossplugin.DefaultSVC().ExecuteTool(ctx, req, opts...) diff --git a/backend/domain/agent/singleagent/service/single_agent_impl.go b/backend/domain/agent/singleagent/service/single_agent_impl.go index 0f98b03bf..8e1da737c 100644 --- a/backend/domain/agent/singleagent/service/single_agent_impl.go +++ b/backend/domain/agent/singleagent/service/single_agent_impl.go @@ -111,6 +111,8 @@ func (s *singleAgentImpl) StreamExecute(ctx context.Context, req *entity.Execute ModelMgr: s.ModelMgr, ModelFactory: s.ModelFactory, CPStore: s.CPStore, + + ConversationID: req.ConversationID, } rn, err := agentflow.BuildAgent(ctx, conf) if err != nil { diff --git a/backend/domain/conversation/agentrun/internal/singleagent_run.go b/backend/domain/conversation/agentrun/internal/singleagent_run.go index fe598676a..dbcac221d 100644 --- a/backend/domain/conversation/agentrun/internal/singleagent_run.go +++ b/backend/domain/conversation/agentrun/internal/singleagent_run.go @@ -49,6 +49,7 @@ func (art *AgentRuntime) AgentStreamExecute(ctx context.Context, imagex imagex.I AgentID: art.GetRunMeta().AgentID, IsDraft: art.GetRunMeta().IsDraft, UserID: art.GetRunMeta().UserID, + ConversationId: art.GetRunMeta().ConversationID, ConnectorID: art.GetRunMeta().ConnectorID, PreRetrieveTools: art.GetRunMeta().PreRetrieveTools, Input: transMessageToSchemaMessage(ctx, []*msgEntity.Message{art.GetInput()}, imagex)[0], diff --git a/backend/domain/plugin/service/exec_tool.go b/backend/domain/plugin/service/exec_tool.go index f1b71c5e3..a83d46c1d 100644 --- a/backend/domain/plugin/service/exec_tool.go +++ b/backend/domain/plugin/service/exec_tool.go @@ -21,6 +21,8 @@ import ( "context" "encoding/json" "fmt" + "github.com/coze-dev/coze-studio/backend/pkg/lang/conv" + "github.com/coze-dev/coze-studio/backend/types/consts" "io" "net/http" "net/url" @@ -123,6 +125,8 @@ func (p *pluginServiceImpl) buildToolExecutor(ctx context.Context, req *ExecuteT impl = &toolExecutor{ execScene: req.ExecScene, userID: req.UserID, + agentID: execOpt.AgentID, + conversationID: execOpt.ConversationID, plugin: pl, tool: tl, projectInfo: execOpt.ProjectInfo, @@ -457,10 +461,13 @@ type ExecuteResponse struct { } type toolExecutor struct { - execScene model.ExecuteScene - userID string - plugin *entity.PluginInfo - tool *entity.ToolInfo + execScene model.ExecuteScene + userID string + agentID int64 + conversationID int64 + + plugin *entity.PluginInfo + tool *entity.ToolInfo projectInfo *entity.ProjectInfo invalidRespProcessStrategy model.InvalidResponseProcessStrategy @@ -738,6 +745,12 @@ func (t *toolExecutor) buildHTTPRequest(ctx context.Context, argMaps map[string] return nil, err } + logId, _ := ctx.Value(consts.CtxLogIDKey).(string) + header.Set("X-Tt-Logid", logId) + header.Set("X-Aiplugin-Connector-Identifier", t.userID) + header.Set("X-AIPlugin-Bot-ID", conv.Int64ToStr(t.agentID)) + header.Set("X-AIPlugin-Conversation-ID", conv.Int64ToStr(t.conversationID)) + httpReq.Header = header if len(bodyBytes) > 0 { diff --git a/backend/domain/workflow/internal/compose/workflow_run.go b/backend/domain/workflow/internal/compose/workflow_run.go index 0268c2d66..3bfd37b64 100644 --- a/backend/domain/workflow/internal/compose/workflow_run.go +++ b/backend/domain/workflow/internal/compose/workflow_run.go @@ -19,6 +19,7 @@ package compose import ( "context" "fmt" + "github.com/coze-dev/coze-studio/backend/types/consts" "runtime/debug" "strconv" "strings" @@ -257,7 +258,7 @@ func (r *WorkflowRunner) Prepare(ctx context.Context) ( if interruptEvent == nil { var logID string - logID, _ = ctx.Value("log-id").(string) + logID, _ = ctx.Value(consts.CtxLogIDKey).(string) wfExec := &entity.WorkflowExecution{ ID: executeID, diff --git a/backend/domain/workflow/internal/execute/event_handle.go b/backend/domain/workflow/internal/execute/event_handle.go index 35b792049..e662bee3a 100644 --- a/backend/domain/workflow/internal/execute/event_handle.go +++ b/backend/domain/workflow/internal/execute/event_handle.go @@ -20,6 +20,7 @@ import ( "context" "errors" "fmt" + "github.com/coze-dev/coze-studio/backend/types/consts" "reflect" "strconv" "time" @@ -113,7 +114,7 @@ func handleEvent(ctx context.Context, event *Event, repo workflow.Repository, if parentNodeID != nil { // root workflow execution has already been created var logID string - logID, _ = ctx.Value("log-id").(string) + logID, _ = ctx.Value(consts.CtxLogIDKey).(string) wfExec := &entity.WorkflowExecution{ ID: exeID, diff --git a/backend/pkg/logs/default.go b/backend/pkg/logs/default.go index 2f81f4a8e..c7916ede3 100644 --- a/backend/pkg/logs/default.go +++ b/backend/pkg/logs/default.go @@ -19,6 +19,7 @@ package logs import ( "context" "fmt" + "github.com/coze-dev/coze-studio/backend/types/consts" "io" "log" "os" @@ -192,7 +193,7 @@ func (ll *defaultLogger) logfCtx(ctx context.Context, lv Level, format *string, return } msg := lv.toString() - logID := ctx.Value("log-id") + logID := ctx.Value(consts.CtxLogIDKey) if logID != nil { msg += fmt.Sprintf("[log-id: %v] ", logID) } diff --git a/backend/types/consts/consts.go b/backend/types/consts/consts.go index 19dd8bd43..543e6605e 100644 --- a/backend/types/consts/consts.go +++ b/backend/types/consts/consts.go @@ -99,6 +99,10 @@ const ( PPStructureAPIURL = "PADDLEOCR_STRUCTURE_API_URL" ) +const ( + CtxLogIDKey = "log-id" +) + const ( ShortcutCommandResourceType = "uri" )