Class: Legion::Extensions::Agentic::Affect::Flow::Helpers::FlowDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/agentic/affect/flow/helpers/flow_detector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFlowDetector

Returns a new instance of FlowDetector.



13
14
15
16
17
18
19
20
21
# File 'lib/legion/extensions/agentic/affect/flow/helpers/flow_detector.rb', line 13

def initialize
  @challenge = 0.5
  @skill = 0.5
  @flow_state = :relaxation
  @flow_score = 0.0
  @consecutive_flow_ticks = 0
  @total_flow_ticks = 0
  @history = []
end

Instance Attribute Details

#challengeObject (readonly)

Returns the value of attribute challenge.



10
11
12
# File 'lib/legion/extensions/agentic/affect/flow/helpers/flow_detector.rb', line 10

def challenge
  @challenge
end

#consecutive_flow_ticksObject (readonly)

Returns the value of attribute consecutive_flow_ticks.



10
11
12
# File 'lib/legion/extensions/agentic/affect/flow/helpers/flow_detector.rb', line 10

def consecutive_flow_ticks
  @consecutive_flow_ticks
end

#flow_scoreObject (readonly)

Returns the value of attribute flow_score.



10
11
12
# File 'lib/legion/extensions/agentic/affect/flow/helpers/flow_detector.rb', line 10

def flow_score
  @flow_score
end

#flow_stateObject (readonly)

Returns the value of attribute flow_state.



10
11
12
# File 'lib/legion/extensions/agentic/affect/flow/helpers/flow_detector.rb', line 10

def flow_state
  @flow_state
end

#historyObject (readonly)

Returns the value of attribute history.



10
11
12
# File 'lib/legion/extensions/agentic/affect/flow/helpers/flow_detector.rb', line 10

def history
  @history
end

#skillObject (readonly)

Returns the value of attribute skill.



10
11
12
# File 'lib/legion/extensions/agentic/affect/flow/helpers/flow_detector.rb', line 10

def skill
  @skill
end

#total_flow_ticksObject (readonly)

Returns the value of attribute total_flow_ticks.



10
11
12
# File 'lib/legion/extensions/agentic/affect/flow/helpers/flow_detector.rb', line 10

def total_flow_ticks
  @total_flow_ticks
end

Instance Method Details

#challenge_skill_balanceObject



62
63
64
# File 'lib/legion/extensions/agentic/affect/flow/helpers/flow_detector.rb', line 62

def challenge_skill_balance
  (@challenge - @skill).abs
end

#deep_flow?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/legion/extensions/agentic/affect/flow/helpers/flow_detector.rb', line 44

def deep_flow?
  in_flow? && @consecutive_flow_ticks >= Constants::DEEP_FLOW_THRESHOLD
end

#flow_effectsObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/legion/extensions/agentic/affect/flow/helpers/flow_detector.rb', line 48

def flow_effects
  if in_flow?
    effects = Constants::FLOW_EFFECTS.dup
    if deep_flow?
      effects[:performance_boost] += 0.05
      effects[:creativity_boost] += 0.1
    end
    effects
  else
    { fatigue_reduction: 1.0, time_dilation: 1.0, performance_boost: 1.0,
      attention_broadening: 1.0, creativity_boost: 1.0 }
  end
end

#flow_percentageObject



84
85
86
87
88
89
# File 'lib/legion/extensions/agentic/affect/flow/helpers/flow_detector.rb', line 84

def flow_percentage
  return 0.0 if @history.empty?

  flow_count = @history.count { |h| h[:state] == :flow }
  (flow_count.to_f / @history.size * 100).round(1)
end

#flow_trendObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/legion/extensions/agentic/affect/flow/helpers/flow_detector.rb', line 66

def flow_trend
  return :insufficient_data if @history.size < 5

  recent = @history.last(10)
  scores = recent.map { |h| h[:flow_score] }
  first_half = scores[0...(scores.size / 2)]
  second_half = scores[(scores.size / 2)..]
  diff = (second_half.sum / second_half.size.to_f) - (first_half.sum / first_half.size.to_f)

  if diff > 0.05
    :entering_flow
  elsif diff < -0.05
    :leaving_flow
  else
    :stable
  end
end

#in_flow?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/legion/extensions/agentic/affect/flow/helpers/flow_detector.rb', line 40

def in_flow?
  @flow_state == :flow
end

#to_hObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/legion/extensions/agentic/affect/flow/helpers/flow_detector.rb', line 91

def to_h
  {
    state:                  @flow_state,
    score:                  @flow_score.round(3),
    challenge:              @challenge.round(3),
    skill:                  @skill.round(3),
    balance:                challenge_skill_balance.round(3),
    in_flow:                in_flow?,
    deep_flow:              deep_flow?,
    consecutive_flow_ticks: @consecutive_flow_ticks,
    total_flow_ticks:       @total_flow_ticks,
    flow_percentage:        flow_percentage,
    trend:                  flow_trend,
    effects:                flow_effects
  }
end

#update(challenge_input:, skill_input:, modifiers: {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/legion/extensions/agentic/affect/flow/helpers/flow_detector.rb', line 23

def update(challenge_input:, skill_input:, modifiers: {})
  @challenge = ema(@challenge, challenge_input.clamp(0.0, 1.0), Constants::FLOW_ALPHA)
  @skill = ema(@skill, skill_input.clamp(0.0, 1.0), Constants::FLOW_ALPHA)

  @flow_state = classify_state
  @flow_score = compute_flow_score(modifiers)

  if @flow_state == :flow
    @consecutive_flow_ticks += 1
    @total_flow_ticks += 1
  else
    @consecutive_flow_ticks = 0
  end

  record_snapshot
end