Skip to content

armgit / ax

Public Imported from GitHub
0

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

Go

Fix remote agent example (#118)

Jaana Dogan1ec9cd89117c
diff --git a/README.md b/README.md
index 1ff1077..d563949 100644
--- a/README.md
+++ b/README.md
@@ -97,19 +97,22 @@ The remote agent runs as a gRPC server implementing `AgentService` on port `:500
**Terminal 2** - Start the AX controller server:
```bash
+# Ensure the agent is registered as a remote agent in ax.yaml.
+cat ax.yaml
+# ...
+registry:
+ remote_agents:
+ - id: "uppercase"
+ name: "Upper Case Agent"
+ description: "Converts text to uppercase."
+ address: "localhost:50051"
+
ax serve
```
-The server exposes the `AXService` on port `:8494`.
+The server exposes the `AXService` on port `:8494` by default.
**Terminal 3** - Register the remote agent and execute:
```bash
-ax register \
- --server localhost:8494 \
- --agent-id uppercase-agent \
- --agent-name "Uppercase Agent" \
- --agent-description "Converts input text to uppercase." \
- --agent-addr localhost:50051
-
ax exec \
--server localhost:8494 \
--id task123 \
diff --git a/agent/remote.go b/agent/remote.go
index 0f04ea2..be210e7 100644
--- a/agent/remote.go
+++ b/agent/remote.go
@@ -18,7 +18,6 @@ import (
"context"
"fmt"
"io"
- "sync"
"time"
"google.golang.org/grpc"
@@ -31,13 +30,7 @@ import (
// RemoteAgent is a gRPC client that implements the Agent interface.
// It communicates with remote agent services over gRPC.
type RemoteAgent struct {
- address string
- conn *grpc.ClientConn
- client proto.AgentServiceClient
- mu sync.Mutex
- reconnect bool
- maxRetries int
- dialOpts []grpc.DialOption
+ cfg RemoteAgentConfig
}
// RemoteAgentConfig configures a remote agent client.
@@ -53,51 +46,35 @@ func NewRemoteAgent(config RemoteAgentConfig) (*RemoteAgent, error) {
if config.Address == "" {
return nil, fmt.Errorf("agent address cannot be empty")
}
-
- agent := &RemoteAgent{
- address: config.Address,
- reconnect: config.Reconnect,
- maxRetries: config.MaxRetries,
- dialOpts: config.DialOpts,
- }
-
- if err := agent.connect(); err != nil {
- return nil, fmt.Errorf("failed to connect to remote agent: %w", err)
- }
-
- return agent, nil
+ return &RemoteAgent{cfg: config}, nil
}
// connect establishes a gRPC connection to the remote agent.
-func (a *RemoteAgent) connect() error {
- a.mu.Lock()
- defer a.mu.Unlock()
-
- if a.conn != nil {
- a.conn.Close()
- }
-
+func (a *RemoteAgent) connect() (*grpc.ClientConn, error) {
// Use provided dial options, or default to insecure credentials
- opts := a.dialOpts
+ opts := a.cfg.DialOpts
if len(opts) == 0 {
opts = []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
}
- conn, err := grpc.NewClient(a.address, opts...)
+ conn, err := grpc.NewClient(a.cfg.Address, opts...)
if err != nil {
- return fmt.Errorf("failed to dial: %w", err)
+ return nil, fmt.Errorf("failed to dial: %w", err)
}
-
- a.conn = conn
- a.client = proto.NewAgentServiceClient(conn)
- return nil
+ return conn, nil
}
// Process handles processing of input content with the remote agent.
func (a *RemoteAgent) Process(ctx context.Context, t *Task, e TaskExecutor, o OutputHandler) error {
ctx = metadata.AppendToOutgoingContext(ctx, "execution-id", t.ID)
+ conn, err := a.connect()
+ if err != nil {
+ return fmt.Errorf("failed to connect: %w", err)
+ }
+ defer conn.Close()
- stream, err := a.client.Process(ctx)
+ client := proto.NewAgentServiceClient(conn)
+ stream, err := client.Process(ctx)
if err != nil {
return fmt.Errorf("failed to create stream: %w", err)
}
@@ -142,7 +119,14 @@ func (a *RemoteAgent) HealthCheck(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
- resp, err := a.client.HealthCheck(ctx, &proto.HealthCheckRequest{})
+ conn, err := a.connect()
+ if err != nil {
+ return fmt.Errorf("failed to connect: %w", err)
+ }
+ defer conn.Close()
+
+ client := proto.NewAgentServiceClient(conn)
+ resp, err := client.HealthCheck(ctx, &proto.HealthCheckRequest{})
if err != nil {
return fmt.Errorf("health check failed: %w", err)
}
@@ -156,11 +140,5 @@ func (a *RemoteAgent) HealthCheck(ctx context.Context) error {
// Close gracefully shuts down the remote agent connection.
func (a *RemoteAgent) Close() error {
- a.mu.Lock()
- defer a.mu.Unlock()
-
- if a.conn != nil {
- return a.conn.Close()
- }
return nil
}
diff --git a/ax.yaml b/ax.yaml
index 813deda..e477531 100644
--- a/ax.yaml
+++ b/ax.yaml
@@ -54,19 +54,16 @@ planner:
# Remote agents configuration (optional)
# List of remote or sandbox agents to register on startup.
registry:
- remote_agents:
- - id: "remote-text-processor"
- name: "Remote Text Processor"
- description: "Converts text to lowercase."
- address: "localhost:50051"
- metadata:
- version: "1.0"
- k8s_sandbox_agents: # TODO: Rename to k8s_sandbox_agents
- - id: "uppercase"
- name: "Upper Case Agent"
- description: "Converts text to uppercase."
- sandbox_template_ref: "uppercase-agent-template"
- container_port: 8494
- use_router: true
- metadata:
- version: "1.0"
+ # remote_agents:
+ # - id: "uppercase"
+ # name: "Upper Case Agent"
+ # description: "Converts text to uppercase."
+ # address: "localhost:50051"
+
+ # k8s_sandbox_agents:
+ # - id: "uppercase"
+ # name: "Upper Case Agent"
+ # description: "Converts text to uppercase."
+ # sandbox_template_ref: "uppercase-agent-template"
+ # container_port: 8494
+ # use_router: true
diff --git a/examples/remote_agent/main.go b/examples/remote_agent/main.go
index 45c1cf6..1ed2b30 100644
--- a/examples/remote_agent/main.go
+++ b/examples/remote_agent/main.go
@@ -16,10 +16,12 @@ package main
import (
"context"
+ "errors"
"fmt"
"io"
"log"
"net"
+ "strings"
"google.golang.org/grpc"
@@ -53,17 +55,23 @@ func (s *server) Process(stream proto.AgentService_ProcessServer) error {
if textContent == nil {
continue
}
- contents = append(contents, &proto.Content{
- Role: "assistant",
- Content: &proto.Content_Text{
- Text: &proto.TextContent{
- Text: "Remote Prefix: " + textContent.Text,
- },
- },
- })
+ contents = append(contents, input)
}
+ if len(contents) == 0 {
+ return errors.New("no text inputs, cannot uppercase")
+ }
+
+ // We don't need to uppercase the whole history.
+ // Only uppercase the last text message.
+ last := contents[len(contents)-1]
+ last.Content = &proto.Content_Text{
+ Text: &proto.TextContent{
+ Text: strings.ToUpper(last.GetText().Text),
+ },
+ }
+
if err := stream.Send(&proto.ProcessResponse{
- Contents: contents,
+ Contents: []*proto.Content{last},
CheckpointId: uuid.New().String(),
}); err != nil {
return err
diff --git a/internal/controller/gemini_planner.go b/internal/controller/gemini_planner.go
index 9326042..6ee20ce 100644
--- a/internal/controller/gemini_planner.go
+++ b/internal/controller/gemini_planner.go
@@ -113,7 +113,6 @@ func (p *geminiPlannerAgent) Process(ctx context.Context, t *agent.Task, e agent
return handler(resp)
}
- t.Inputs = append(t.Inputs, outputs...)
nextAgentID, err := p.process(ctx, t, outputCapturer)
if err != nil {
return err
@@ -128,6 +127,7 @@ func (p *geminiPlannerAgent) Process(ctx context.Context, t *agent.Task, e agent
}, handler); err != nil {
return err
}
+ t.Inputs = append(t.Inputs, outputs...)
return nil
}