Class: LanguageOperator::Dsl::MainDefinition

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/language_operator/dsl/main_definition.rb

Overview

Main execution block for agents (DSL v1)

Defines the imperative entry point for agent execution. The main block receives agent inputs and returns agent outputs. Within the block, agents can call tasks using execute_task(), use standard Ruby control flow (if/else, loops), and handle errors with standard Ruby exceptions.

This replaces the declarative workflow/step model with an imperative programming model centered on organic functions (tasks).

Examples:

Simple main block

main do |inputs|
  result = execute_task(:fetch_data, inputs: inputs)
  execute_task(:process_data, inputs: result)
end

Main block with control flow

main do |inputs|
  data = execute_task(:fetch_data, inputs: inputs)

  if data[:count] > 100
    execute_task(:send_alert, inputs: { count: data[:count] })
  end

  execute_task(:save_results, inputs: data)
end

Main block with error handling

main do |inputs|
  begin
    result = execute_task(:risky_operation, inputs: inputs)
    { success: true, result: result }
  rescue => e
    logger.error("Operation failed: #{e.message}")
    { success: false, error: e.message }
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#logger

Constructor Details

#initializeMainDefinition

Initialize a new main definition



50
51
52
# File 'lib/language_operator/dsl/main_definition.rb', line 50

def initialize
  @execute_block = nil
end

Instance Attribute Details

#execute_blockObject (readonly)

Returns the value of attribute execute_block.



47
48
49
# File 'lib/language_operator/dsl/main_definition.rb', line 47

def execute_block
  @execute_block
end

Instance Method Details

#call(inputs, context) ⇒ Object

Execute the main block with given inputs

Parameters:

  • inputs (Hash)

    Agent input parameters

  • context (Object)

    Execution context that provides execute_task method

Returns:

  • (Object)

    Result from main block

Raises:

  • (RuntimeError)

    If main block is not defined

  • (ArgumentError)

    If inputs is not a Hash



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/language_operator/dsl/main_definition.rb', line 79

def call(inputs, context)
  raise 'Main block not defined. Use execute { |inputs| ... } to define it.' unless @execute_block
  raise ArgumentError, "inputs must be a Hash, got #{inputs.class}" unless inputs.is_a?(Hash)

  logger.info('Executing main block', inputs_keys: inputs.keys)

  result = logger.timed('Main execution') do
    # Execute block in context to provide access to execute_task
    context.instance_exec(inputs, &@execute_block)
  end

  logger.info('Main block completed')
  result
rescue StandardError => e
  logger.error('Main block execution failed',
               error: e.class.name,
               message: e.message,
               backtrace: e.backtrace&.first(5))
  raise
end

#defined?Boolean

Check if main block is defined

Returns:

  • (Boolean)

    True if execute block is set



103
104
105
# File 'lib/language_operator/dsl/main_definition.rb', line 103

def defined?
  !@execute_block.nil?
end

#execute {|inputs| ... } ⇒ void

This method returns an undefined value.

Define the main execution block

Examples:

execute do |inputs|
  result = execute_task(:my_task, inputs: inputs)
  result
end

Yields:

  • (inputs)

    Block that receives agent inputs and returns agent outputs

Yield Parameters:

  • inputs (Hash)

    Agent input parameters

Yield Returns:

  • (Object)

    Agent output (typically a Hash)

Raises:

  • (ArgumentError)


65
66
67
68
69
70
# File 'lib/language_operator/dsl/main_definition.rb', line 65

def execute(&block)
  raise ArgumentError, 'Main block is required' unless block

  @execute_block = block
  logger.debug('Main block defined', arity: block.arity)
end