Skip to content

armgit / ax

Public Imported from GitHub
0

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

Go

Process handler should take the current session ID

JBD15d062d60fb5
diff --git a/README.md b/README.md
index c7f94f6..c530d1b 100644
--- a/README.md
+++ b/README.md
@@ -243,7 +243,7 @@ import (
)
// Define your process function using callback handler
-processFunc := func(ctx context.Context, inputs []*proto.Content, handler agent.OutputHandler) error {
+processFunc := func(ctx context.Context, sessionID string, inputs []*proto.Content, handler agent.OutputHandler) error {
for _, content := range inputs {
output := &proto.Content{
Role: "assistant",
diff --git a/agent/agent.go b/agent/agent.go
index 5060801..3b1a2b2 100644
--- a/agent/agent.go
+++ b/agent/agent.go
@@ -22,7 +22,7 @@ type Agent interface {
// Process handles processing of input content.
// It calls the output handler for each piece of content generated.
// The handler may be called multiple times during processing.
- Process(ctx context.Context, inputs []*proto.Content, handler OutputHandler) error
+ Process(ctx context.Context, sessionID string, inputs []*proto.Content, handler OutputHandler) error
// StreamLifecycle streams lifecycle events from the agent.
// It calls the handler for each lifecycle event emitted.
diff --git a/agent/local.go b/agent/local.go
index 96f9dbb..4abd8cf 100644
--- a/agent/local.go
+++ b/agent/local.go
@@ -11,7 +11,7 @@ import (
// It implements the Agent interface for agents running in the same process as the dispatcher.
type LocalAgent struct {
id string
- processFunc func(ctx context.Context, inputs []*proto.Content, handler OutputHandler) error
+ processFunc func(ctx context.Context, sessionID string, inputs []*proto.Content, handler OutputHandler) error
lifecycleFunc func(ctx context.Context, handler LifecycleHandler) error
healthCheckFunc func(ctx context.Context) error
}
@@ -19,7 +19,7 @@ type LocalAgent struct {
// LocalAgentConfig configures a local agent.
type LocalAgentConfig struct {
ID string
- ProcessFunc func(ctx context.Context, inputs []*proto.Content, handler OutputHandler) error
+ ProcessFunc func(ctx context.Context, sessionID string, inputs []*proto.Content, handler OutputHandler) error
LifecycleFunc func(ctx context.Context, handler LifecycleHandler) error
HealthCheckFunc func(ctx context.Context) error
}
@@ -53,8 +53,8 @@ func NewLocalAgent(config LocalAgentConfig) (*LocalAgent, error) {
}
// Process handles processing of input content with callback handler.
-func (a *LocalAgent) Process(ctx context.Context, inputs []*proto.Content, handler OutputHandler) error {
- return a.processFunc(ctx, inputs, handler)
+func (a *LocalAgent) Process(ctx context.Context, sessionID string, inputs []*proto.Content, handler OutputHandler) error {
+ return a.processFunc(ctx, sessionID, inputs, handler)
}
// StreamLifecycle streams lifecycle events using callback handler.
diff --git a/agent/remote.go b/agent/remote.go
index 16f92c8..3b4515f 100644
--- a/agent/remote.go
+++ b/agent/remote.go
@@ -9,6 +9,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
+ "google.golang.org/grpc/metadata"
"github.com/google/gar/proto"
)
@@ -85,7 +86,11 @@ func (a *RemoteAgent) connect() error {
}
// Process handles processing of input content with the remote agent.
-func (a *RemoteAgent) Process(ctx context.Context, inputs []*proto.Content, handler OutputHandler) error {
+func (a *RemoteAgent) Process(ctx context.Context, sessionID string, inputs []*proto.Content, handler OutputHandler) error {
+ // Add session_id to gRPC metadata
+ md := metadata.Pairs("session-id", sessionID)
+ ctx = metadata.NewOutgoingContext(ctx, md)
+
stream, err := a.client.Process(ctx)
if err != nil {
return fmt.Errorf("failed to create stream: %w", err)
diff --git a/examples/local_agent/main.go b/examples/local_agent/main.go
index 631f3ca..67b5e68 100644
--- a/examples/local_agent/main.go
+++ b/examples/local_agent/main.go
@@ -69,7 +69,7 @@ func main() {
// createEchoAgent creates a simple echo agent that repeats input with a prefix.
func createEchoAgent(id string) (*agent.LocalAgent, error) {
- processFunc := func(ctx context.Context, inputs []*proto.Content, handler agent.OutputHandler) error {
+ processFunc := func(ctx context.Context, sessionID string, inputs []*proto.Content, handler agent.OutputHandler) error {
// Process each input and call handler with response
for _, content := range inputs {
// Echo the content back with a prefix
@@ -77,7 +77,7 @@ func createEchoAgent(id string) (*agent.LocalAgent, error) {
Role: "assistant",
Type: content.Type,
Mimetype: content.Mimetype,
- Data: fmt.Sprintf("Echo: %s", strings.ToUpper(content.Data)),
+ Data: fmt.Sprintf("Echo (session %s): %s", sessionID, strings.ToUpper(content.Data)),
}
// Call handler with the response
diff --git a/internal/controller/loop.go b/internal/controller/loop.go
index 8b85cd8..7dbe038 100644
--- a/internal/controller/loop.go
+++ b/internal/controller/loop.go
@@ -211,7 +211,7 @@ func (e *LoopExecutor) executeTask(ctx context.Context, session *Session, ag age
}()
// Process inputs with the agent
- if err := ag.Process(ctx, task.Input, outputHandler); err != nil {
+ if err := ag.Process(ctx, session.ID, task.Input, outputHandler); err != nil {
cancelLifecycle()
return nil, fmt.Errorf("agent process failed: %w", err)
}
diff --git a/python/example_agent.py b/python/example_agent.py
index b8c360e..abb9da7 100644
--- a/python/example_agent.py
+++ b/python/example_agent.py
@@ -9,14 +9,14 @@ from gar import Agent
import proto.gar_pb2 as pb2
-def process(inputs):
+def process(session_id, inputs):
"""Process incoming content list and yield responses"""
for content in inputs:
yield pb2.Content(
role="assistant",
type="text",
mimetype="text/plain",
- data=f"Python processed: {content.data.upper()}"
+ data=f"Python processed (session {session_id}): {content.data.upper()}"
)
diff --git a/python/gar.py b/python/gar.py
index 9e5743c..d512204 100644
--- a/python/gar.py
+++ b/python/gar.py
@@ -16,7 +16,7 @@ class Agent:
Agent provides a simple framework for building Python agents.
Usage:
- def process(inputs):
+ def process(session_id, inputs):
for content in inputs:
yield Content(role="assistant", type="text",
mimetype="text/plain", data=f"Processed: {content.data}")
@@ -37,7 +37,7 @@ class Agent:
Args:
agent_id: Unique identifier for this agent
- process_func: Function that takes a list of Content and yields Content responses
+ process_func: Function that takes (session_id: str, inputs: list) and yields Content responses
lifecycle_func: Optional function that yields LifecycleEvent objects
health_check_func: Optional function that returns (healthy: bool, message: str, metadata: dict)
"""
@@ -52,11 +52,15 @@ class Agent:
class AgentServicer(pb2_grpc.AgentServiceServicer):
def Process(self, request_iterator, context):
+ # Extract session_id from gRPC metadata
+ metadata = dict(context.invocation_metadata())
+ session_id = metadata.get('session-id', '')
+
# Collect all content into a list
inputs = list(request_iterator)
- # Process the list of content
- for response in agent.process_func(inputs):
+ # Process the list of content with session_id
+ for response in agent.process_func(session_id, inputs):
if response:
yield response
@@ -137,7 +141,7 @@ def create_agent(
Args:
agent_id: Unique identifier for this agent
- process_func: Function that takes a list of Content and yields Content responses
+ process_func: Function that takes (session_id: str, inputs: list) and yields Content responses
lifecycle_func: Optional function that yields LifecycleEvent objects
health_check_func: Optional function that returns (healthy: bool, message: str, metadata: dict)
port: Port to listen on (default: 50051)