fix(multi-agent): do not allow to call ourselves

Signed-off-by: mudler <mudler@localai.io>
This commit is contained in:
mudler
2025-03-28 16:51:07 +01:00
parent 05af5d9695
commit 8ac6f68568
3 changed files with 21 additions and 9 deletions

View File

@@ -65,6 +65,8 @@ func Actions(a *state.AgentConfig) func(ctx context.Context, pool *state.AgentPo
return func(ctx context.Context, pool *state.AgentPool) []types.Action {
allActions := []types.Action{}
agentName := a.Name
for _, a := range a.Actions {
var config map[string]string
if err := json.Unmarshal([]byte(a.Config), &config); err != nil {
@@ -72,7 +74,7 @@ func Actions(a *state.AgentConfig) func(ctx context.Context, pool *state.AgentPo
continue
}
a, err := Action(a.Name, config, pool)
a, err := Action(a.Name, agentName, config, pool)
if err != nil {
continue
}
@@ -83,7 +85,7 @@ func Actions(a *state.AgentConfig) func(ctx context.Context, pool *state.AgentPo
}
}
func Action(name string, config map[string]string, pool *state.AgentPool) (types.Action, error) {
func Action(name, agentName string, config map[string]string, pool *state.AgentPool) (types.Action, error) {
var a types.Action
var err error
@@ -125,7 +127,7 @@ func Action(name string, config map[string]string, pool *state.AgentPool) (types
case ActionCounter:
a = actions.NewCounter(config)
case ActionCallAgents:
a = actions.NewCallAgent(config, pool.InternalAPI())
a = actions.NewCallAgent(config, agentName, pool.InternalAPI())
case ActionShellcommand:
a = actions.NewShell(config)
default:

View File

@@ -10,14 +10,16 @@ import (
"github.com/sashabaranov/go-openai/jsonschema"
)
func NewCallAgent(config map[string]string, pool *state.AgentPoolInternalAPI) *CallAgentAction {
func NewCallAgent(config map[string]string, agentName string, pool *state.AgentPoolInternalAPI) *CallAgentAction {
return &CallAgentAction{
pool: pool,
pool: pool,
myName: agentName,
}
}
type CallAgentAction struct {
pool *state.AgentPoolInternalAPI
pool *state.AgentPoolInternalAPI
myName string
}
func (a *CallAgentAction) Run(ctx context.Context, params types.ActionParams) (types.ActionResult, error) {
@@ -57,9 +59,17 @@ func (a *CallAgentAction) Run(ctx context.Context, params types.ActionParams) (t
func (a *CallAgentAction) Definition() types.ActionDefinition {
allAgents := a.pool.AllAgents()
agents := []string{}
for _, ag := range allAgents {
if ag != a.myName {
agents = append(agents, ag)
}
}
description := "Use this tool to call another agent. Available agents and their roles are:"
for _, agent := range allAgents {
for _, agent := range agents {
agentConfig := a.pool.GetConfig(agent)
if agentConfig == nil {
continue
@@ -74,7 +84,7 @@ func (a *CallAgentAction) Definition() types.ActionDefinition {
"agent_name": {
Type: jsonschema.String,
Description: "The name of the agent to call.",
Enum: allAgents,
Enum: agents,
},
"message": {
Type: jsonschema.String,