feat(plugin): append special headers (agentId, userId, conversationId and logId) to plugin http request, which can be used by plugin server (#2191)

This commit is contained in:
大鹏
2025-09-19 14:00:44 +08:00
committed by GitHub
parent 315a93f8c0
commit b05004f188
14 changed files with 60 additions and 8 deletions

View File

@ -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)

View File

@ -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
}
}

View File

@ -112,6 +112,8 @@ type ExecuteRequest struct {
History []*schema.Message
ResumeInfo *InterruptInfo
PreCallTools []*agentrun.ToolsRetriever
ConversationID int64
}
type AgentIdentity struct {

View File

@ -37,6 +37,7 @@ type AgentRuntime struct {
AgentVersion string
UserID string
AgentID int64
ConversationId int64
IsDraft bool
SpaceID int64
ConnectorID int64

View File

@ -80,6 +80,8 @@ func (c *impl) buildSingleAgentStreamExecuteReq(ctx context.Context, agentRuntim
}
}),
ResumeInfo: agentRuntime.ResumeInfo,
ConversationID: agentRuntime.ConversationId,
}
}

View File

@ -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

View File

@ -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...)

View File

@ -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 {

View File

@ -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],

View File

@ -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 {

View File

@ -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,

View File

@ -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,

View File

@ -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)
}

View File

@ -99,6 +99,10 @@ const (
PPStructureAPIURL = "PADDLEOCR_STRUCTURE_API_URL"
)
const (
CtxLogIDKey = "log-id"
)
const (
ShortcutCommandResourceType = "uri"
)