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?, #call, #language_agnostic?, #placeholder?, #posture?, #quantifies?, #to_s, usable

Constructor Details

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

Returns a new instance of ColangFlow.

Raises:



20
21
22
23
24
25
26
# 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
  raise ColangError, "no flow named #{flow_name.inspect}" unless program.flow(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



34
35
36
# File 'lib/vangrail/rails/colang_flow.rb', line 34

def cache_key(_text, _context)
  nil
end

#decide(text, context) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/vangrail/rails/colang_flow.rb', line 38

def decide(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

#offline?Boolean

Actions registered on the flow can hit the network. The default offline? is true; this rail is not that case.

Returns:

  • (Boolean)


30
31
32
# File 'lib/vangrail/rails/colang_flow.rb', line 30

def offline?
  false
end