Class: LittleGhost::Workflow::Invocation

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

Overview

Hold one lazy agent call inside a workflow composition. Workflow implementations normally use only its output method or return the object as the final invocation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reference:, participant:, input:, history:, context:, policies:, owner:) ⇒ Invocation

:nodoc:



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/little_ghost/workflow.rb', line 60

def initialize(reference:, participant:, input:, history:, context:, policies:, owner:) # :nodoc:
  @reference = reference
  @participant = participant
  @input = input
  @history = history
  @context = context
  @policies = policies
  @owner = owner
  @mutex = Mutex.new
  @consumed = false
  @closed = false
end

Instance Attribute Details

#resultObject (readonly)

:nodoc:



58
59
60
# File 'lib/little_ghost/workflow.rb', line 58

def result
  @result
end

Instance Method Details

#closeObject

:nodoc:



120
121
122
123
124
125
126
# File 'lib/little_ghost/workflow.rb', line 120

def close # :nodoc:
  @mutex.synchronize do
    return if @closed

    @closed = true
  end
end

#consumed?Boolean

:nodoc:

Returns:

  • (Boolean)


116
117
118
# File 'lib/little_ghost/workflow.rb', line 116

def consumed? # :nodoc:
  @mutex.synchronize { @consumed }
end

#each(checkpoint: nil) ⇒ Object

:nodoc:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/little_ghost/workflow.rb', line 73

def each(checkpoint: nil) # :nodoc:
  return enum_for(__method__, checkpoint:) unless block_given?

  @mutex.synchronize do
    raise Error, "workflow invocation is already closed" if @closed
    raise ProtocolError, "workflow invocation was already consumed" if @consumed

    @consumed = true
  end
  execution = @owner.send(
    :execute_workflow_invocation,
    reference: @reference,
    participant: @participant,
    input: @input,
    history: @history,
    context: @context,
    policies: @policies,
    checkpoint:
  ) { |event| yield event }
  @result = execution.result
  @step = execution.step
  @steps = execution.result.steps
  execution.events.each do |event|
    yield event
  end
  @result
end

#outputObject

Consumes this invocation when necessary and returns RunResult#output.

A structured agent returns its validated value; an ordinary agent returns response text. Intermediate usage is recorded for the workflow total.



105
106
107
108
109
110
111
112
113
114
# File 'lib/little_ghost/workflow.rb', line 105

def output
  @intermediate = true
  unless consumed?
    each do |event|
      @owner.send(:emit_workflow_event, event) if event.type.to_s.start_with?("assembly_")
    end
    @owner.send(:record_workflow_steps, @steps)
  end
  result&.output
end