Skip to content

armgit / ax

Public Imported from GitHub
0

Agent Executor (AX) is Google's open agentic orchestration runtime

Go

Enable skills in the Gemini planner (#149)

Jaana Dogan637b27d14dcf
diff --git a/internal/controller/gemini.go b/internal/controller/gemini.go
index fed8290..411c7a7 100644
--- a/internal/controller/gemini.go
+++ b/internal/controller/gemini.go
@@ -133,6 +133,7 @@ func (a *GeminiAgent) Close() error {
type Tool interface {
Name() string
FuncDecl() []*genai.Tool
+ SystemPrompt() string
HandleCall(ctx context.Context, fc *genai.FunctionCall, o agent.OutputHandler) error
HandleExecute(ctx context.Context, fc *genai.FunctionCall, approved bool, o agent.OutputHandler) error
}
@@ -143,6 +144,10 @@ func (t *BashTool) Name() string {
return "bash"
}
+func (t *BashTool) SystemPrompt() string {
+ return ""
+}
+
func (t *BashTool) FuncDecl() []*genai.Tool {
osInfo := fmt.Sprintf("User's Operating System: %s (%s)", runtime.GOOS, runtime.GOARCH)
description := fmt.Sprintf("OS specific bash execution tool. %s. Generate commands appropriate for this OS. Returns the command output or error. Never produce code, only use existing command line programs available in the system.", osInfo)
@@ -273,13 +278,13 @@ type SkillsTool struct {
executor *skills.Executor
}
-func NewSkillsTool(dir string) (*SkillsTool, error) {
+func NewSkillsTool(dir string) (Tool, error) {
executor, err := skills.NewExecutor(dir)
if err != nil {
return nil, err
}
if !executor.HasSkills() {
- return nil, nil
+ return &NoopTool{}, nil
}
return &SkillsTool{executor: executor}, nil
}
@@ -304,6 +309,12 @@ func (t *SkillsTool) HandleCall(ctx context.Context, fc *genai.FunctionCall, o a
if err != nil {
return err
}
+
+ if fc.Name == "activate_skill" {
+ // Skill activation is always approved.
+ return t.HandleExecute(ctx, fc, true, o)
+ }
+
if fc.Name == "run_skill_script" {
skill, _ := fc.Args["skill"].(string)
script, _ := fc.Args["script"].(string)
@@ -336,24 +347,49 @@ func (t *SkillsTool) HandleCall(ctx context.Context, fc *genai.FunctionCall, o a
}},
})
}
- return o(&proto.AgentOutputs{
- Messages: []*proto.Message{
- {
- Role: "model",
- Content: &proto.Content{
- Content: &proto.Content_FunctionCall{
- FunctionCall: &proto.FunctionCallContent{
- Id: fc.ID,
- Name: fc.Name,
- Args: argsStruct,
+ return nil
+}
+
+func (t *SkillsTool) HandleExecute(ctx context.Context, fc *genai.FunctionCall, approved bool, o agent.OutputHandler) error {
+ if fc.Name == "activate_skill" {
+ var output string
+ result, err := t.executor.HandleCall(ctx, fc)
+ if err != nil {
+ output = "Error: " + err.Error()
+ } else {
+ var parts []string
+ if result.Stdout != "" {
+ parts = append(parts, result.Stdout)
+ }
+ if result.Stderr != "" {
+ parts = append(parts, "Stderr: "+result.Stderr)
+ }
+ output = strings.Join(parts, "\n")
+ }
+
+ respStruct, err := structpb.NewStruct(map[string]any{"result": output})
+ if err != nil {
+ return fmt.Errorf("failed to convert function response to structpb: %w", err)
+ }
+
+ return o(&proto.AgentOutputs{
+ Messages: []*proto.Message{
+ {
+ Role: "model",
+ Content: &proto.Content{
+ Content: &proto.Content_FunctionResponse{
+ FunctionResponse: &proto.FunctionResponseContent{
+ Name: fc.Name,
+ Response: respStruct,
+ Id: fc.ID,
+ },
},
},
},
- }},
- })
-}
+ },
+ })
+ }
-func (t *SkillsTool) HandleExecute(ctx context.Context, fc *genai.FunctionCall, approved bool, o agent.OutputHandler) error {
if !approved {
// Declined, nothing to do in terms of executing any commands.
// But we still have to finish with a function response,
@@ -441,6 +477,10 @@ func (t *NoopTool) Name() string {
return ""
}
+func (t *NoopTool) SystemPrompt() string {
+ return ""
+}
+
func (t *NoopTool) FuncDecl() []*genai.Tool {
return []*genai.Tool{}
}
diff --git a/internal/controller/gemini_planner.go b/internal/controller/gemini_planner.go
index 690089b..d48f963 100644
--- a/internal/controller/gemini_planner.go
+++ b/internal/controller/gemini_planner.go
@@ -17,6 +17,7 @@ package controller
import (
"context"
"fmt"
+ "io"
"os"
"github.com/golang/protobuf/ptypes/duration"
@@ -86,35 +87,27 @@ Guidelines:
return nil, fmt.Errorf("failed to create Gemini client: %w", err)
}
- // TODO(jbd): Enable skills tool.
- return &geminiPlannerAgent{
- client: client,
- bashTool: &BashTool{},
- registry: registry,
- config: config,
- }, nil
-}
-
-func (p *geminiPlannerAgent) Connect(ctx context.Context, execID string, start *proto.AgentStart, e agent.Executor, handler agent.OutputHandler) error {
- var outputs []*proto.Message
- var outputCapturer = func(resp *proto.AgentOutputs) error {
- outputs = append(outputs, resp.Messages...)
- return handler(resp)
- }
- nextAgentID, err := p.process(ctx, start, outputCapturer)
- if err != nil {
- return err
+ skillsTool, err := NewSkillsTool(config.SkillsDir)
+ if err != nil && err != io.EOF {
+ return nil, fmt.Errorf("failed to initialize skills tool: %w", err)
}
- if nextAgentID == "" {
- return nil
+
+ p := &geminiPlannerAgent{
+ client: client,
+ bashTool: &BashTool{},
+ skillsTool: skillsTool,
+ registry: registry,
+ config: config,
}
- if err := e.Exec(ctx, nextAgentID, &proto.AgentStart{
- AgentId: nextAgentID,
- Messages: append(start.Messages, outputs...),
- }, handler); err != nil {
- return err
+
+ if sp := skillsTool.SystemPrompt(); sp != "" {
+ p.config.GeminiConfig.SystemPrompt += "\n\n" + sp
}
- return nil
+ return p, nil
+}
+
+func (p *geminiPlannerAgent) Connect(ctx context.Context, execID string, start *proto.AgentStart, e agent.Executor, handler agent.OutputHandler) error {
+ return p.loop(ctx, start, e, handler)
}
func (p *geminiPlannerAgent) HealthCheck(ctx context.Context) error {
@@ -125,16 +118,55 @@ func (p *geminiPlannerAgent) Close() error {
return nil
}
-func (p *geminiPlannerAgent) process(ctx context.Context, start *proto.AgentStart, handler agent.OutputHandler) (agentID string, err error) {
+func (p *geminiPlannerAgent) loop(ctx context.Context, start *proto.AgentStart, e agent.Executor, handler agent.OutputHandler) (err error) {
+ var outputs []*proto.Message
+ var outputCapturer = func(resp *proto.AgentOutputs) error {
+ outputs = append(outputs, resp.Messages...)
+ return handler(resp)
+ }
+
+ for {
+ nextAgentID, keepLooping, err := p.process(ctx, start, outputCapturer)
+ if err != nil {
+ return err
+ }
+ if keepLooping {
+ // Some function calls require multiple turns to complete,
+ // e.g. the skill activation and running.
+ // Allow the loop to continue without switching agents or user input.
+ start.Messages = append(start.Messages, outputs...)
+ outputs = nil
+ continue
+ }
+
+ if nextAgentID == "" {
+ // No agent to delegate, we are done.
+ return nil
+ }
+ start = &proto.AgentStart{
+ AgentId: nextAgentID,
+ Messages: append(start.Messages, outputs...),
+ }
+ outputs = nil
+ if err := e.Exec(ctx, nextAgentID, start, handler); err != nil {
+ return err
+ }
+ }
+}
+
+func (p *geminiPlannerAgent) process(ctx context.Context, start *proto.AgentStart, handler agent.OutputHandler) (agentID string, keepLooping bool, err error) {
tools, err := agentsToTools(p.registry, p.bashTool, p.skillsTool)
if err != nil {
- return "", fmt.Errorf("failed to convert agents to tools: %w", err)
+ return "", false, fmt.Errorf("failed to convert agents to tools: %w", err)
}
inputs := start.Messages
if fc, approved := p.handleConfirmationAnswer(inputs); fc != nil {
- if p.bashTool.Name() == fc.Name {
- return "", p.bashTool.HandleExecute(ctx, fc, approved, handler)
+ if fc.Name == p.bashTool.Name() {
+ return "", false, p.bashTool.HandleExecute(ctx, fc, approved, handler)
+ }
+ if fc.Name == "run_skill_script" {
+ return "", false, p.skillsTool.HandleExecute(ctx, fc, approved, handler)
}
}
@@ -155,17 +187,17 @@ func (p *geminiPlannerAgent) process(ctx context.Context, start *proto.AgentStar
})
if err != nil {
- return "", fmt.Errorf("failed to generate in planner: %w", err)
+ return "", false, fmt.Errorf("failed to generate in planner: %w", err)
}
if len(resp.Candidates) == 0 {
- return "", fmt.Errorf("no candidates from Gemini in planner")
+ return "", false, fmt.Errorf("no candidates from Gemini in planner")
}
candidate := resp.Candidates[0]
if candidate.Content == nil || candidate.Content.Parts == nil {
if candidate.FinishReason == genai.FinishReasonStop {
- return "", nil // No more tasks
+ return "", false, nil // No more tasks
}
- return "", fmt.Errorf("no content in candidates from Gemini")
+ return "", false, fmt.Errorf("no content in candidates from Gemini")
}
// Look for function calls in the response
@@ -185,7 +217,7 @@ func (p *geminiPlannerAgent) process(ctx context.Context, start *proto.AgentStar
},
}},
}); err != nil {
- return "", err
+ return "", false, err
}
}
@@ -193,15 +225,17 @@ func (p *geminiPlannerAgent) process(ctx context.Context, start *proto.AgentStar
fc.ID = uuid.NewString()
switch fc.Name {
case p.bashTool.Name():
- return "", p.bashTool.HandleCall(ctx, fc, handler)
- case "run_skill_script", "activate_skill":
- return "", p.skillsTool.HandleCall(ctx, fc, handler)
+ return "", false, p.bashTool.HandleCall(ctx, fc, handler)
+ case "run_skill_script":
+ return "", false, p.skillsTool.HandleCall(ctx, fc, handler)
+ case "activate_skill":
+ return "", true, p.skillsTool.HandleCall(ctx, fc, handler)
default:
- return fc.Name, nil
+ return fc.Name, false, nil
}
}
}
- return "", nil
+ return "", false, nil
}
func (p *geminiPlannerAgent) handleConfirmationAnswer(inputs []*proto.Message) (*genai.FunctionCall, bool) {