Skip to content

armgit / ax

Public Imported from GitHub
0

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

Go

Introduce Tool for native tools and skills support (#124)

Jaana Dogan6abded93d160
diff --git a/ax.yaml b/ax.yaml
index 4e94e94..f3c7228 100644
--- a/ax.yaml
+++ b/ax.yaml
@@ -30,25 +30,10 @@ health_check:
# Planner configuration (optional)
planner:
- # Gemini planner configuration
- # The Gemini planner uses Google's Gemini models for intelligent agent selection.
+ # The Gemini planner uses Google's Gemini models for tools and agent selection.
gemini:
- # Model name (e.g., "gemini-3-flash")
- model: "gemini-3-flash-preview"
-
- # Temperature for generation (0.0 to 1.0)
- temperature: 0.7
-
- # Maximum output tokens
- max_tokens: 8192
-
- # Request timeout (e.g., "30s", "1m")
- timeout: 60s
-
- # Custom system prompt for the planner (optional)
- system_prompt: ""
-
- # Directory to load standalone skills from
+ model: "gemini-3-flash-preview" # model name
+ timeout: "60s"
skills_dir: "./examples/skills"
# Remote agents configuration (optional)
diff --git a/internal/controller/gemini.go b/internal/controller/gemini.go
index d2147ee..eac6efe 100644
--- a/internal/controller/gemini.go
+++ b/internal/controller/gemini.go
@@ -19,12 +19,16 @@ import (
"errors"
"fmt"
"os"
+ "os/exec"
"runtime"
+ "strings"
"github.com/golang/protobuf/ptypes/duration"
"github.com/google/ax/agent"
+ "github.com/google/ax/internal/skills"
"github.com/google/ax/proto"
"google.golang.org/genai"
+ "google.golang.org/protobuf/types/known/structpb"
)
// GeminiAgent implements task.Agent using Gemini.
@@ -124,24 +128,27 @@ func (a *GeminiAgent) Close() error {
return nil
}
-const bashToolName = "bash"
-
-func newBashTool() *bashTool {
- return &bashTool{}
+type Tool interface {
+ Name() string
+ FuncDecl() []*genai.Tool
+ HandleCall(ctx context.Context, fc *genai.FunctionCall, o agent.OutputHandler) error
+ HandleExecute(ctx context.Context, fc *genai.FunctionCall, approved bool, o agent.OutputHandler) error
}
-// bashTool is the built-in tool that allows
-// planner to execute general purpose bash commands.
-type bashTool struct{}
+type BashTool struct{}
+
+func (t *BashTool) Name() string {
+ return "bash"
+}
-func (f *bashTool) funcDecl() *genai.Tool {
+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)
- return &genai.Tool{
+ return []*genai.Tool{{
FunctionDeclarations: []*genai.FunctionDeclaration{
{
- Name: bashToolName,
+ Name: t.Name(),
Description: description,
Parameters: &genai.Schema{
Type: genai.TypeObject,
@@ -155,5 +162,297 @@ func (f *bashTool) funcDecl() *genai.Tool {
},
},
},
+ }}
+}
+
+func (t *BashTool) HandleCall(ctx context.Context, fc *genai.FunctionCall, o agent.OutputHandler) error {
+ command, _ := fc.Args["command"].(string)
+ argsStruct, err := structpb.NewStruct(fc.Args)
+ if err != nil {
+ return err
+ }
+ return o(&proto.ProcessResponse{
+ Contents: []*proto.Content{
+ {
+ Content: &proto.Content_FunctionCall{
+ FunctionCall: &proto.FunctionCallContent{
+ Id: fc.ID,
+ Name: fc.Name,
+ Args: argsStruct,
+ },
+ },
+ },
+ {
+ Content: &proto.Content_Confirmation{
+ Confirmation: &proto.ConfirmationContent{
+ Id: fc.ID,
+ Question: fmt.Sprintf("Can I run %q?", command),
+ },
+ },
+ }},
+ })
+}
+
+func (t *BashTool) 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,
+ // not to keep the previously log function call hanging forever.
+ return o(&proto.ProcessResponse{
+ Contents: []*proto.Content{
+ {
+ Role: "assistant",
+ Content: &proto.Content_FunctionResponse{
+ FunctionResponse: &proto.FunctionResponseContent{
+ Name: fc.Name,
+ Id: fc.ID,
+ },
+ },
+ },
+ {
+ Role: "assistant",
+ Content: &proto.Content_Text{
+ Text: &proto.TextContent{
+ Text: "Okay.",
+ },
+ },
+ },
+ },
+ })
+ }
+
+ output, err := execute(fc.Args)
+ if err != nil {
+ return err
+ }
+ 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.ProcessResponse{
+ Contents: []*proto.Content{
+ {
+ Role: "assistant",
+ Content: &proto.Content_FunctionResponse{
+ FunctionResponse: &proto.FunctionResponseContent{
+ Name: fc.Name,
+ Response: respStruct,
+ Id: fc.ID,
+ },
+ },
+ },
+ {
+ Role: "assistant",
+ Content: &proto.Content_Text{
+ Text: &proto.TextContent{
+ Text: output,
+ },
+ },
+ },
+ },
+ })
+}
+
+type SkillsTool struct {
+ executor *skills.Executor
+}
+
+func NewSkillsTool(dir string) (*SkillsTool, error) {
+ executor, err := skills.NewExecutor(dir)
+ if err != nil {
+ return nil, err
+ }
+ if !executor.HasSkills() {
+ return nil, nil
+ }
+ return &SkillsTool{executor: executor}, nil
+}
+
+func (t *SkillsTool) Name() string {
+ return ""
+}
+
+func (t *SkillsTool) SystemPrompt() string {
+ return t.executor.SystemPrompt()
+}
+
+func (t *SkillsTool) FuncDecl() []*genai.Tool {
+ if !t.executor.HasSkills() {
+ return []*genai.Tool{}
+ }
+ return []*genai.Tool{skills.BuildTool(t.executor.SkillNames())}
+}
+
+func (t *SkillsTool) HandleCall(ctx context.Context, fc *genai.FunctionCall, o agent.OutputHandler) error {
+ argsStruct, err := structpb.NewStruct(fc.Args)
+ if err != nil {
+ return err
+ }
+ if fc.Name == "run_skill_script" {
+ skill, _ := fc.Args["skill"].(string)
+ script, _ := fc.Args["script"].(string)
+ question := fmt.Sprintf("Can I run script %q from skill %q?", script, skill)
+
+ return o(&proto.ProcessResponse{
+ Contents: []*proto.Content{
+ {
+ Content: &proto.Content_FunctionCall{
+ FunctionCall: &proto.FunctionCallContent{
+ Id: fc.ID,
+ Name: fc.Name,
+ Args: argsStruct,
+ },
+ },
+ },
+ {
+ Content: &proto.Content_Confirmation{
+ Confirmation: &proto.ConfirmationContent{
+ Id: fc.ID,
+ Question: question,
+ },
+ },
+ }},
+ })
+ }
+ return o(&proto.ProcessResponse{
+ Contents: []*proto.Content{
+ {
+ Content: &proto.Content_FunctionCall{
+ FunctionCall: &proto.FunctionCallContent{
+ Id: fc.ID,
+ Name: fc.Name,
+ Args: argsStruct,
+ },
+ },
+ }},
+ })
+}
+
+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,
+ // not to keep the previously log function call hanging forever.
+ return o(&proto.ProcessResponse{
+ Contents: []*proto.Content{
+ {
+ Role: "assistant",
+ Content: &proto.Content_FunctionResponse{
+ FunctionResponse: &proto.FunctionResponseContent{
+ Name: fc.Name,
+ Id: fc.ID,
+ },
+ },
+ },
+ {
+ Role: "assistant",
+ Content: &proto.Content_Text{
+ Text: &proto.TextContent{
+ Text: "Okay.",
+ },
+ },
+ },
+ },
+ })
+ }
+
+ var output string
+ if fc.Name == "run_skill_script" {
+ result := t.executor.HandleCall(ctx, fc)
+ if result != nil && result.FunctionResponse != nil {
+ resultMap := result.FunctionResponse.Response
+ if body, ok := resultMap["instructions"]; ok {
+ output = body.(string)
+ } else if errStr, ok := resultMap["error"]; ok {
+ output = "Error: " + errStr.(string)
+ } else {
+ if so, ok := resultMap["stdout"].(string); ok && so != "" {
+ output += so
+ }
+ if se, ok := resultMap["stderr"].(string); ok && se != "" {
+ if output != "" {
+ output += "\n"
+ }
+ output += "Stderr: " + se
+ }
+ }
+ } else {
+ output = "Error: nil response from executor"
+ }
+ }
+
+ 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.ProcessResponse{
+ Contents: []*proto.Content{
+ {
+ Role: "assistant",
+ Content: &proto.Content_FunctionResponse{
+ FunctionResponse: &proto.FunctionResponseContent{
+ Name: fc.Name,
+ Response: respStruct,
+ Id: fc.ID,
+ },
+ },
+ },
+ {
+ Role: "assistant",
+ Content: &proto.Content_Text{
+ Text: &proto.TextContent{
+ Text: output,
+ },
+ },
+ },
+ },
+ })
+}
+
+type NoopTool struct {
+}
+
+func (t *NoopTool) Name() string {
+ return ""
+}
+
+func (t *NoopTool) FuncDecl() []*genai.Tool {
+ return []*genai.Tool{}
+}
+
+func (t *NoopTool) HandleCall(ctx context.Context, fc *genai.FunctionCall, o agent.OutputHandler) error {
+ return errors.New("cannot call noop tool")
+}
+
+func (t *NoopTool) HandleExecute(ctx context.Context, fc *genai.FunctionCall, approved bool, o agent.OutputHandler) error {
+ return errors.New("cannot execute noop tool")
+}
+
+func execute(args map[string]any) (string, error) {
+ command, ok := args["command"].(string)
+ if !ok {
+ return "", fmt.Errorf("command parameter missing or invalid")
}
+
+ // Execute the command.
+ var cmd *exec.Cmd
+ if runtime.GOOS == "windows" {
+ cmd = exec.Command("cmd", "/C", command)
+ } else {
+ cmd = exec.Command("sh", "-c", command)
+ }
+
+ output, err := cmd.CombinedOutput()
+ if err != nil {
+ // Return both the error and any output that was produced
+ return fmt.Sprintf("Error: %v\nOutput: %s\n\n", err, output), nil
+ }
+
+ result := strings.TrimSpace(string(output))
+ if result == "" {
+ return "Command executed successfully (no output)", nil
+ }
+ result += "\n\n"
+
+ return result, nil
}
diff --git a/internal/controller/gemini_planner.go b/internal/controller/gemini_planner.go
index 6ee20ce..51089f5 100644
--- a/internal/controller/gemini_planner.go
+++ b/internal/controller/gemini_planner.go
@@ -4,17 +4,12 @@ import (
"context"
"fmt"
"os"
- "os/exec"
- "runtime"
- "strings"
"github.com/golang/protobuf/ptypes/duration"
"github.com/google/ax/agent"
- "github.com/google/ax/internal/skills"
"github.com/google/ax/proto"
"github.com/google/uuid"
"google.golang.org/genai"
- "google.golang.org/protobuf/types/known/structpb"
)
// GeminiPlannerConfig configures the Gemini-based planner.
@@ -26,11 +21,11 @@ type GeminiPlannerConfig struct {
// geminiPlannerAgent implements task.Agent using Gemini.
type geminiPlannerAgent struct {
- config GeminiPlannerConfig
- client *genai.Client
- bashTool *bashTool
- registry *Registry
- skillExec *skills.Executor
+ config GeminiPlannerConfig
+ client *genai.Client
+ bashTool Tool
+ skillsTool Tool
+ registry *Registry
}
// NewGeminiPlannerAgent creates a new Gemini-based agent.
@@ -48,10 +43,6 @@ func NewGeminiPlannerAgent(ctx context.Context, registry *Registry, config Gemin
}
}
- if config.MaxSteps == 0 {
- config.MaxSteps = 5
- }
-
// Default system prompt
if config.GeminiConfig.SystemPrompt == "" {
config.GeminiConfig.SystemPrompt = `You are an intelligent orchestrator. Your role is to analyze the conversation history and user requests, then select the most appropriate agent to handle the task.
@@ -81,28 +72,13 @@ Guidelines:
return nil, fmt.Errorf("failed to create Gemini client: %w", err)
}
- skillsDir := config.SkillsDir
- if skillsDir == "" {
- skillsDir = os.Getenv("SKILLS_DIR")
- }
- if skillsDir == "" {
- skillsDir = skills.DefaultDir()
- }
-
- skillExec, err := skills.NewExecutor(client, config.GeminiConfig.Model, skillsDir)
- if err != nil {
- return nil, err
- }
+ // TODO(jbd): Enable skills tool.
- if skillExec.HasSkills() {
- // config.SystemPrompt += "\n\n" + skillExec.SystemPrompt()
- }
return &geminiPlannerAgent{
- client: client,
- bashTool: newBashTool(),
- registry: registry,
- config: config,
- skillExec: skillExec,
+ client: client,
+ bashTool: &BashTool{},
+ registry: registry,
+ config: config,
}, nil
}
@@ -140,17 +116,16 @@ func (p *geminiPlannerAgent) Close() error {
}
func (p *geminiPlannerAgent) process(ctx context.Context, t *agent.Task, handler agent.OutputHandler) (agentID string, err error) {
- tools, err := agentsToTools(p.bashTool, p.registry)
+ tools, err := agentsToTools(p.registry, p.bashTool, p.skillsTool)
if err != nil {
return "", fmt.Errorf("failed to convert agents to tools: %w", err)
}
- if p.skillExec.HasSkills() {
- // tools = append(tools, skills.BuildTool(p.skillExec.SkillNames()))
- }
inputs := t.Inputs
if fc, approved := p.handleConfirmationAnswer(inputs); fc != nil {
- return "", p.handleExecute(ctx, fc, approved, handler)
+ if p.bashTool.Name() == fc.Name {
+ return "", p.bashTool.HandleExecute(ctx, fc, approved, handler)
+ }
}
contents := protoToContents(inputs)
@@ -205,10 +180,10 @@ func (p *geminiPlannerAgent) process(ctx context.Context, t *agent.Task, handler
if fc := part.FunctionCall; fc != nil {
fc.ID = uuid.NewString()
switch fc.Name {
- case bashToolName:
- return "", p.handleBash(fc, handler)
+ case p.bashTool.Name():
+ return "", p.bashTool.HandleCall(ctx, fc, handler)
case "run_skill_script", "activate_skill":
- return "", p.handleSkill(ctx, fc, handler)
+ return "", p.skillsTool.HandleCall(ctx, fc, handler)
default:
return fc.Name, nil
}
@@ -217,198 +192,6 @@ func (p *geminiPlannerAgent) process(ctx context.Context, t *agent.Task, handler
return "", nil
}
-func (p *geminiPlannerAgent) handleSkill(ctx context.Context, fc *genai.FunctionCall, o agent.OutputHandler) error {
- if fc.Name == "run_skill_script" {
- skill, _ := fc.Args["skill"].(string)
- script, _ := fc.Args["script"].(string)
- question := fmt.Sprintf("Can I run script %q from skill %q?", script, skill)
-
- argsStruct, err := structpb.NewStruct(fc.Args)
- if err != nil {
- return err
- }
- return o(&proto.ProcessResponse{
- Contents: []*proto.Content{
- {
- Content: &proto.Content_FunctionCall{
- FunctionCall: &proto.FunctionCallContent{
- Id: fc.ID,
- Name: fc.Name,
- Args: argsStruct,
- },
- },
- },
- {
- Content: &proto.Content_Confirmation{
- Confirmation: &proto.ConfirmationContent{
- Id: fc.ID,
- Question: question,
- },
- },
- }},
- })
- }
-
- resultPart := p.skillExec.HandleCall(ctx, fc)
- var output string
- if resultPart != nil && resultPart.FunctionResponse != nil {
- resultMap := resultPart.FunctionResponse.Response
- if body, ok := resultMap["instructions"]; ok {
- output = body.(string)
- } else if errStr, ok := resultMap["error"]; ok {
- output = "Error: " + errStr.(string)
- } else {
- if so, ok := resultMap["stdout"].(string); ok && so != "" {
- output += so
- }
- if se, ok := resultMap["stderr"].(string); ok && se != "" {
- if output != "" {
- output += "\n"
- }
- output += "Stderr: " + se
- }
- if output == "" {
- output = "Command executed successfully (no output)"
- }
- }
- } else {
- output = "Error: nil response from executor"
- }
-
- 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.ProcessResponse{
- Contents: []*proto.Content{{
- Role: "assistant",
- Content: &proto.Content_FunctionResponse{
- FunctionResponse: &proto.FunctionResponseContent{
- Name: fc.Name,
- Response: respStruct,
- Id: fc.ID,
- },
- },
- }},
- })
-}
-
-func (p *geminiPlannerAgent) handleBash(fc *genai.FunctionCall, o agent.OutputHandler) error {
- command, _ := fc.Args["command"].(string)
- argsStruct, err := structpb.NewStruct(fc.Args)
- if err != nil {
- return err
- }
- return o(&proto.ProcessResponse{
- Contents: []*proto.Content{
- {
- Content: &proto.Content_FunctionCall{
- FunctionCall: &proto.FunctionCallContent{
- Id: fc.ID,
- Name: fc.Name,
- Args: argsStruct,
- },
- },
- },
- {
- Content: &proto.Content_Confirmation{
- Confirmation: &proto.ConfirmationContent{
- Id: fc.ID,
- Question: fmt.Sprintf("Can I run %q?", command),
- },
- },
- }},
- })
-
-}
-
-func (p *geminiPlannerAgent) 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,
- // not to keep the previously log function call hanging forever.
- return o(&proto.ProcessResponse{
- Contents: []*proto.Content{
- {
- Role: "assistant",
- Content: &proto.Content_FunctionResponse{
- FunctionResponse: &proto.FunctionResponseContent{
- Name: fc.Name,
- Id: fc.ID,
- },
- },
- },
- {
- Role: "assistant",
- Content: &proto.Content_Text{
- Text: &proto.TextContent{
- Text: "Okay.",
- },
- },
- },
- },
- })
- }
-
- var output string
- switch fc.Name {
- case "bash":
- var err error
- output, err = execute(fc.Args)
- if err != nil {
- return err
- }
- case "run_skill_script":
- result := p.skillExec.HandleCall(ctx, fc)
- if result != nil && result.FunctionResponse != nil {
- resultMap := result.FunctionResponse.Response
- if body, ok := resultMap["instructions"]; ok {
- output = body.(string)
- } else if errStr, ok := resultMap["error"]; ok {
- output = "Error: " + errStr.(string)
- } else {
- if so, ok := resultMap["stdout"].(string); ok && so != "" {
- output += so
- }
- if se, ok := resultMap["stderr"].(string); ok && se != "" {
- if output != "" {
- output += "\n"
- }
- output += "Stderr: " + se
- }
- }
- } else {
- output = "Error: nil response from executor"
- }
- }
- 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.ProcessResponse{
- Contents: []*proto.Content{
- {
- Role: "assistant",
- Content: &proto.Content_FunctionResponse{
- FunctionResponse: &proto.FunctionResponseContent{
- Name: fc.Name,
- Response: respStruct,
- Id: fc.ID,
- },
- },
- },
- {
- Role: "assistant",
- Content: &proto.Content_Text{
- Text: &proto.TextContent{
- Text: output,
- },
- },
- },
- },
- })
-}
-
func (p *geminiPlannerAgent) handleConfirmationAnswer(inputs []*proto.Content) (*genai.FunctionCall, bool) {
var conf *proto.ConfirmationContent
var approved bool
@@ -461,7 +244,7 @@ func (p *geminiPlannerAgent) handleConfirmationAnswer(inputs []*proto.Content) (
}
// agentsToTools converts registry agents to Gemini function declarations.
-func agentsToTools(bashTool *bashTool, registry *Registry) ([]*genai.Tool, error) {
+func agentsToTools(registry *Registry, nativeTools ...Tool) ([]*genai.Tool, error) {
healthyAgents := registry.ListHealthy()
var tools []*genai.Tool
@@ -482,8 +265,11 @@ func agentsToTools(bashTool *bashTool, registry *Registry) ([]*genai.Tool, error
FunctionDeclarations: []*genai.FunctionDeclaration{funcDecl},
})
}
- if bashTool != nil {
- tools = append(tools, bashTool.funcDecl())
+ for _, nativeTool := range nativeTools {
+ if nativeTool == nil {
+ continue
+ }
+ tools = append(tools, nativeTool.FuncDecl()...)
}
return tools, nil
}
@@ -556,32 +342,3 @@ func protoToContents(inputs []*proto.Content) []*genai.Content {
}
return contents
}
-
-func execute(args map[string]any) (string, error) {
- command, ok := args["command"].(string)
- if !ok {
- return "", fmt.Errorf("command parameter missing or invalid")
- }
-
- // Execute the command.
- var cmd *exec.Cmd
- if runtime.GOOS == "windows" {
- cmd = exec.Command("cmd", "/C", command)
- } else {
- cmd = exec.Command("sh", "-c", command)
- }
-
- output, err := cmd.CombinedOutput()
- if err != nil {
- // Return both the error and any output that was produced
- return fmt.Sprintf("Error: %v\nOutput: %s\n\n", err, output), nil
- }
-
- result := strings.TrimSpace(string(output))
- if result == "" {
- return "Command executed successfully (no output)", nil
- }
- result += "\n\n"
-
- return result, nil
-}
diff --git a/internal/skills/exec.go b/internal/skills/exec.go
index 3a7ad33..437938e 100644
--- a/internal/skills/exec.go
+++ b/internal/skills/exec.go
@@ -22,49 +22,16 @@ import (
"google.golang.org/genai"
)
-// UpdateKind identifies the type of an UpdateEvent.
-type UpdateKind int
-
-const (
- UpdateSkillActivated UpdateKind = iota
- UpdateScriptRunning
- UpdateScriptDone
-)
-
-// UpdateEvent describes a state change during execution.
-type UpdateEvent struct {
- Kind UpdateKind
- Skill string
- Script string // UpdateScriptRunning and UpdateScriptDone
- ExitCode int // UpdateScriptDone only
-}
-
// Executor runs an agentskills.io-compatible agentic loop.
type Executor struct {
- client *genai.Client
model string
- cfg *genai.GenerateContentConfig
-
- byName map[string]Skill
- names []string
- onUpdate func(UpdateEvent)
-}
-
-// OnUpdate registers a callback that is called when a skill is activated or a
-// script is run. Replaces any previously registered callback.
-func (e *Executor) OnUpdate(fn func(UpdateEvent)) {
- e.onUpdate = fn
-}
-
-func (e *Executor) update(ev UpdateEvent) {
- if e.onUpdate != nil {
- e.onUpdate(ev)
- }
+ byName map[string]Skill
+ names []string
}
// NewExecutor discovers skills in dir, then creates an Executor. The caller is
// responsible for creating the client and choosing a model.
-func NewExecutor(client *genai.Client, model, dir string) (*Executor, error) {
+func NewExecutor(dir string) (*Executor, error) {
if dir == "" {
dir = os.Getenv("SKILLS_DIR")
}
@@ -87,54 +54,12 @@ func NewExecutor(client *genai.Client, model, dir string) (*Executor, error) {
byName[s.Name] = s
}
- si := "You are a helpful AI assistant with access to a set of skills.\n" +
- "When a user task matches one or more skills, call activate_skill to load\n" +
- "its full instructions before answering.\n\n" +
- SystemPrompt(found)
-
- cfg := &genai.GenerateContentConfig{
- SystemInstruction: genai.NewContentFromText(si, genai.RoleUser),
- Tools: []*genai.Tool{BuildTool(names)},
- }
-
return &Executor{
- client: client,
byName: byName,
names: names,
- cfg: cfg,
- model: model,
}, nil
}
-// Run sends task to the model and returns the final text response, activating
-// skills and executing scripts as requested by the model along the way.
-func (e *Executor) Run(ctx context.Context, prompt string) (string, error) {
- history := []*genai.Content{
- genai.NewContentFromText(prompt, genai.RoleUser),
- }
-
- for {
- resp, err := e.client.Models.GenerateContent(ctx, e.model, history, e.cfg)
- if err != nil {
- return "", err
- }
- if len(resp.Candidates) > 0 && resp.Candidates[0].Content != nil {
- history = append(history, resp.Candidates[0].Content)
- }
-
- calls := resp.FunctionCalls()
- if len(calls) == 0 {
- return resp.Text(), nil
- }
-
- var results []*genai.Part
- for _, call := range calls {
- results = append(results, e.HandleCall(ctx, call))
- }
- history = append(history, genai.NewContentFromParts(results, genai.RoleUser))
- }
-}
-
// HandleCall processes an 'activate_skill' or 'run_skill_script' call.
// It returns a genai.Part containing the function response.
func (e *Executor) HandleCall(ctx context.Context, call *genai.FunctionCall) *genai.Part {
@@ -152,7 +77,6 @@ func (e *Executor) HandleCall(ctx context.Context, call *genai.FunctionCall) *ge
if !ok {
return respond(map[string]any{"error": "unknown skill: " + name})
}
- e.update(UpdateEvent{Kind: UpdateSkillActivated, Skill: name})
body, err := s.Body()
if err != nil {
return respond(map[string]any{"error": err.Error()})
@@ -181,12 +105,10 @@ func (e *Executor) HandleCall(ctx context.Context, call *genai.FunctionCall) *ge
return respond(map[string]any{"error": "unknown skill: " + skillName})
}
- e.update(UpdateEvent{Kind: UpdateScriptRunning, Skill: skillName, Script: script})
result, err := s.RunScript(ctx, script, args)
if err != nil {
return respond(map[string]any{"error": err.Error()})
}
- e.update(UpdateEvent{Kind: UpdateScriptDone, Skill: skillName, Script: script, ExitCode: result.ExitCode})
return respond(map[string]any{
"stdout": result.Stdout,
"stderr": result.Stderr,