executor

package
v0.0.28 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

README

EVE Executor Package

Unified execution interface for semantic actions across all EVE-based projects.

Overview

The executor package provides a standardized way to execute semantic actions (Schema.org based) with support for multiple execution types, retry logic, and extensible architecture.

Features

  • Unified Interface: Single Executor interface for all execution types
  • Registry Pattern: Pluggable executor implementations with automatic selection
  • Context Support: Full context.Context integration for cancellation and timeouts
  • Structured Results: Rich Result type with metadata, timing, and error details
  • Built-in Executors:
    • HTTPExecutor: Execute HTTP-based semantic actions
    • CommandExecutor: Execute shell commands
  • Extensible: Easy to add custom executors

Basic Usage

package main

import (
    "context"
    "fmt"
    "time"

    "eve.evalgo.org/executor"
    "eve.evalgo.org/semantic"
)

func main() {
    // Create a registry
    registry := executor.NewRegistry()

    // Register executors
    registry.Register(executor.NewHTTPExecutor())
    registry.Register(executor.NewCommandExecutor())

    // Create an action
    action := &semantic.SemanticScheduledAction{
        Type: "RetrieveAction",
        Object: &semantic.SemanticObject{
            ContentUrl: "https://api.example.com/data",
        },
    }

    // Execute with context
    ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    defer cancel()

    result, err := registry.Execute(ctx, action)
    if err != nil {
        fmt.Printf("Execution failed: %v\n", err)
        return
    }

    fmt.Printf("Status: %s\n", result.Status)
    fmt.Printf("Output: %s\n", result.Output)
    fmt.Printf("Duration: %v\n", result.Duration)
}

Action Types

HTTP Actions

The HTTP executor supports these action types:

  • SearchAction → GET
  • RetrieveAction → GET
  • SendAction → POST
  • CreateAction → POST
  • UpdateAction → PUT
  • ReplaceAction → PUT
  • DeleteAction → DELETE

Example:

action := &semantic.SemanticScheduledAction{
    Type: "CreateAction",
    Object: &semantic.SemanticObject{
        ContentUrl: "https://api.example.com/users",
        Text: `{"name": "John Doe"}`,
        EncodingFormat: "application/json",
    },
}
Command Actions

The command executor handles shell command execution:

  • URLs starting with exec://, command://, or shell://

Example:

action := &semantic.SemanticScheduledAction{
    Type: "Action",
    Object: &semantic.SemanticObject{
        ContentUrl: "exec://ls -la /tmp",
    },
}

Custom Executors

Create custom executors by implementing the Executor interface:

type MyExecutor struct {
    // Your fields
}

func (e *MyExecutor) Name() string {
    return "my-executor"
}

func (e *MyExecutor) CanHandle(action *semantic.SemanticScheduledAction) bool {
    // Return true if this executor can handle the action
    return action.Object != nil && strings.HasPrefix(action.Object.ContentUrl, "myprotocol://")
}

func (e *MyExecutor) Execute(ctx context.Context, action *semantic.SemanticScheduledAction) (*Result, error) {
    result := &Result{
        StartTime: time.Now(),
        Status: StatusRunning,
        Metadata: make(map[string]interface{}),
    }

    // Your execution logic here

    result.Status = StatusCompleted
    result.Output = "execution result"
    result.EndTime = time.Now()
    result.Duration = result.EndTime.Sub(result.StartTime)

    return result, nil
}

Then register it:

registry.Register(&MyExecutor{})

Result Structure

type Result struct {
    Output    string                 // Primary execution result
    Status    ExecutionStatus        // pending, running, completed, failed, cancelled
    Metadata  map[string]interface{} // Additional execution information
    Error     *ExecutionError        // Detailed error if failed
    StartTime time.Time              // When execution began
    EndTime   time.Time              // When execution completed
    Duration  time.Duration          // Execution duration
}

Migration from Existing Code

Before (project-specific Execute)
func Execute(req *Request) (string, error) {
    resp, err := http.Get(req.URL)
    if err != nil {
        return "", err
    }
    defer resp.Body.Close()

    body, _ := io.ReadAll(resp.Body)
    return string(body), nil
}
After (using eve/executor)
func Execute(req *Request) (string, error) {
    registry := executor.NewRegistry()
    registry.Register(executor.NewHTTPExecutor())

    action := &semantic.SemanticScheduledAction{
        Type: "RetrieveAction",
        Object: &semantic.SemanticObject{
            ContentUrl: req.URL,
        },
    }

    result, err := registry.Execute(context.Background(), action)
    if err != nil {
        return "", err
    }

    return result.Output, nil
}

Projects Using This Package

This executor package consolidates Execute implementations from:

  • claude-tools
  • fetcher
  • graphium
  • http2amqp
  • pg-queue
  • px-containers
  • pxgraphservice
  • pxtool
  • when

Advanced Features (Coming Soon)

  • Retry policies with exponential/linear backoff
  • Execution hooks (before/after/error)
  • Storage abstraction for persistence
  • Dependency resolution
  • Workflow execution

License

Part of the EVE library - see main LICENSE file.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BackoffStrategy

type BackoffStrategy string

BackoffStrategy determines delay between retries

const (
	BackoffExponential BackoffStrategy = "exponential"
	BackoffLinear      BackoffStrategy = "linear"
	BackoffFixed       BackoffStrategy = "fixed"
)

type CommandExecutor

type CommandExecutor struct {
	Shell string
}

CommandExecutor executes shell commands

func NewCommandExecutor

func NewCommandExecutor() *CommandExecutor

NewCommandExecutor creates a new command executor

func (*CommandExecutor) CanHandle

func (e *CommandExecutor) CanHandle(action *semantic.SemanticScheduledAction) bool

CanHandle determines if this executor can process the action

func (*CommandExecutor) Execute

Execute runs the command and returns the result

func (*CommandExecutor) Name

func (e *CommandExecutor) Name() string

Name returns the executor's identifier

type ExecuteOptions

type ExecuteOptions struct {
	// Context for cancellation/timeout
	Context context.Context

	// RetryPolicy defines retry behavior
	RetryPolicy *RetryPolicy

	// Dependencies are actions that must complete first
	Dependencies []string

	// Storage for persisting results
	Storage Storage

	// Hooks for lifecycle events
	Hooks *ExecutionHooks
}

ExecuteWithOptions provides advanced execution control

type ExecutionError

type ExecutionError struct {
	Message string
	Code    string
	Details map[string]interface{}
}

ExecutionError provides detailed error information

func (*ExecutionError) Error

func (e *ExecutionError) Error() string

Error implements the error interface for ExecutionError

type ExecutionHooks

type ExecutionHooks struct {
	BeforeExecute func(ctx context.Context, action *semantic.SemanticScheduledAction) error
	AfterExecute  func(ctx context.Context, action *semantic.SemanticScheduledAction, result *Result) error
	OnError       func(ctx context.Context, action *semantic.SemanticScheduledAction, err error) error
}

ExecutionHooks allows customization of execution lifecycle

type ExecutionStatus

type ExecutionStatus string

ExecutionStatus represents the state of execution

const (
	StatusPending   ExecutionStatus = "pending"
	StatusRunning   ExecutionStatus = "running"
	StatusCompleted ExecutionStatus = "completed"
	StatusFailed    ExecutionStatus = "failed"
	StatusCancelled ExecutionStatus = "cancelled"
)

type Executor

type Executor interface {
	// Execute runs an action and returns the result
	Execute(ctx context.Context, action *semantic.SemanticScheduledAction) (*Result, error)

	// CanHandle determines if this executor can process the action
	CanHandle(action *semantic.SemanticScheduledAction) bool

	// Name returns the executor's identifier
	Name() string
}

Executor is the unified interface for all execution types

type HTTPExecutor

type HTTPExecutor struct {
	Client *http.Client
}

HTTPExecutor executes HTTP-based semantic actions

func NewHTTPExecutor

func NewHTTPExecutor() *HTTPExecutor

NewHTTPExecutor creates a new HTTP executor with default settings

func (*HTTPExecutor) CanHandle

func (e *HTTPExecutor) CanHandle(action *semantic.SemanticScheduledAction) bool

CanHandle determines if this executor can process the action

func (*HTTPExecutor) Execute

Execute runs the HTTP action and returns the result

func (*HTTPExecutor) Name

func (e *HTTPExecutor) Name() string

Name returns the executor's identifier

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry manages executor implementations

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new executor registry

func (*Registry) Execute

func (r *Registry) Execute(ctx context.Context, action *semantic.SemanticScheduledAction) (*Result, error)

Execute finds the appropriate executor and runs the action

func (*Registry) Register

func (r *Registry) Register(executor Executor)

Register adds an executor to the registry

type Result

type Result struct {
	// Output is the primary execution result (stdout, response body, etc.)
	Output string

	// Status indicates the execution status
	Status ExecutionStatus

	// Metadata contains additional execution information
	Metadata map[string]interface{}

	// Error contains detailed error information if execution failed
	Error *ExecutionError

	// StartTime when execution began
	StartTime time.Time

	// EndTime when execution completed
	EndTime time.Time

	// Duration of execution
	Duration time.Duration
}

Result contains the execution output and metadata

type RetryPolicy

type RetryPolicy struct {
	MaxAttempts int
	Backoff     BackoffStrategy
}

RetryPolicy defines retry behavior

type Storage

type Storage interface {
	Save(ctx context.Context, actionID string, result *Result) error
	Load(ctx context.Context, actionID string) (*Result, error)
}

Storage interface for persisting execution state

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL