Class: Vangrail::Rails::ColangFlow

Inherits:
Vangrail::Rail show all
Defined in:
lib/vangrail/rails/colang_flow.rb

Overview

A Colang flow as a rail.

This is what lets a configuration folder written for the Python toolkit run here: rails.input.flows: [self check input] becomes one of these, the flow executes in Ruby, and its bot refuse to respond / stop becomes a blocked Result carrying the refusal text.

Not memoizable by default. A flow can call any registered action, and this class cannot know whether one of them reads the passages or the history.

Constant Summary

Constants inherited from Vangrail::Rail

Vangrail::Rail::DEFAULT_SIDES, Vangrail::Rail::SIDES

Instance Attribute Summary collapse

Attributes inherited from Vangrail::Rail

#name, #sides

Instance Method Summary collapse

Methods inherited from Vangrail::Rail

#applies_to?, #offline?, #placeholder?, #to_s

Constructor Details

#initialize(flow_name:, program:, actions:, name: nil, sides: Rail::SIDES) ⇒ ColangFlow

Returns a new instance of ColangFlow.



20
21
22
23
24
# File 'lib/vangrail/rails/colang_flow.rb', line 20

def initialize(flow_name:, program:, actions:, name: nil, sides: Rail::SIDES)
  super(name: name || flow_name, sides: sides)
  @flow_name = flow_name
  @interpreter = Colang::Interpreter.new(program: program, actions: actions)
end

Instance Attribute Details

#flow_nameObject (readonly)

Returns the value of attribute flow_name.



18
19
20
# File 'lib/vangrail/rails/colang_flow.rb', line 18

def flow_name
  @flow_name
end

#interpreterObject (readonly)

Returns the value of attribute interpreter.



18
19
20
# File 'lib/vangrail/rails/colang_flow.rb', line 18

def interpreter
  @interpreter
end

Instance Method Details

#cache_key(_text, _context) ⇒ Object



26
27
28
# File 'lib/vangrail/rails/colang_flow.rb', line 26

def cache_key(_text, _context)
  nil
end

#call(text, context) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/vangrail/rails/colang_flow.rb', line 30

def call(text, context)
  outcome = interpreter.run(flow_name, context.merge(text: text))
  case outcome.status
  when :blocked then block(content: outcome.content, reason: outcome.reason || flow_name)
  when :modified then modify(outcome.content, reason: outcome.reason || flow_name)
  else pass
  end
end