Class: LittleGhost::CodeMode::AgentRuntime

Inherits:
Object
  • Object
show all
Defined in:
lib/little_ghost/code_mode/runtime.rb

Overview

:nodoc:

Defined Under Namespace

Classes: State

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent:, declaration:) ⇒ AgentRuntime

Returns a new instance of AgentRuntime.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/little_ghost/code_mode/runtime.rb', line 10

def initialize(agent:, declaration:)
  @agent = agent
  @declaration = declaration.transform_keys(&:to_sym)
  implementation = CodeMode.resolve_engine(@declaration.fetch(:engine, :ruby))
  @engine = implementation.is_a?(Class) ? implementation.new : implementation
  @except = @declaration[:except]
  @limits = @declaration.fetch(:limits, {})
  @sandbox_declaration = @declaration.fetch(:sandbox, :native)
  @catalog_broker = Broker.new(agent:, except: @except)
  @states = {}
  @states_mutex = Mutex.new
  @closed = false
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



24
25
26
# File 'lib/little_ghost/code_mode/runtime.rb', line 24

def engine
  @engine
end

Instance Method Details

#brokerObject



26
# File 'lib/little_ghost/code_mode/runtime.rb', line 26

def broker = @catalog_broker

#catalogObject



27
# File 'lib/little_ghost/code_mode/runtime.rb', line 27

def catalog = @catalog_broker.catalog

#close(context: nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/little_ghost/code_mode/runtime.rb', line 60

def close(context: nil)
  states = @states_mutex.synchronize do
    selected = if context
      [@states[context]].compact
    else
      @closed = true
      @states.values
    end
    selected.each { |state| state.mutex.synchronize { state.closing = true } }
    selected
  end
  states.reverse_each { |state| close_state(state) }
  @states_mutex.synchronize do
    if context
      @states.delete(context) if states.include?(@states[context])
    else
      states.each { |state| @states.delete_if { |_key, current| current.equal?(state) } }
    end
  end
end

#execute(source:, context:, **options) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/little_ghost/code_mode/runtime.rb', line 30

def execute(source:, context:, **options)
  state = state_for(context)
  with_control(state) do
    bind_broker(state.broker, context)
    with_delivery(state.broker) do
      session_for(state).execute(source:, catalog: state.broker.catalog, context:, **options)
    end
  end
end

#instructionsObject



28
# File 'lib/little_ghost/code_mode/runtime.rb', line 28

def instructions = engine.instructions(catalog:)

#stop(context:, **options) ⇒ Object

Raises:



50
51
52
53
54
55
56
57
58
# File 'lib/little_ghost/code_mode/runtime.rb', line 50

def stop(context:, **options)
  state = existing_state(context)
  raise ToolError, "there is no active code-mode program" unless state

  with_control(state) do
    bind_broker(state.broker, context)
    with_delivery(state.broker) { session_for(state).stop(context:, **options) }
  end
end

#wait(context:, **options) ⇒ Object

Raises:



40
41
42
43
44
45
46
47
48
# File 'lib/little_ghost/code_mode/runtime.rb', line 40

def wait(context:, **options)
  state = existing_state(context)
  raise ToolError, "there is no active code-mode program" unless state

  with_control(state) do
    bind_broker(state.broker, context)
    with_delivery(state.broker) { session_for(state).wait(context:, **options) }
  end
end