Class: RubyLLM::ResponsesSession

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_sdk/ruby_llm_patches/responses_api_patch.rb

Overview

Manages state for OpenAI Responses API stateful conversations. Tracks response IDs, session validity, and failure recovery.

Constant Summary collapse

RESPONSE_ID_TTL =

5 minutes

300
MAX_FAILURES =
2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response_id: nil, last_activity: nil, failure_count: 0, disabled: false) ⇒ ResponsesSession

Returns a new instance of ResponsesSession.



27
28
29
30
31
32
# File 'lib/swarm_sdk/ruby_llm_patches/responses_api_patch.rb', line 27

def initialize(response_id: nil, last_activity: nil, failure_count: 0, disabled: false)
  @response_id = response_id
  @last_activity = last_activity
  @failure_count = failure_count
  @disabled = disabled
end

Instance Attribute Details

#failure_countObject (readonly)

Returns the value of attribute failure_count.



25
26
27
# File 'lib/swarm_sdk/ruby_llm_patches/responses_api_patch.rb', line 25

def failure_count
  @failure_count
end

#last_activityObject (readonly)

Returns the value of attribute last_activity.



25
26
27
# File 'lib/swarm_sdk/ruby_llm_patches/responses_api_patch.rb', line 25

def last_activity
  @last_activity
end

#response_idObject (readonly)

Returns the value of attribute response_id.



25
26
27
# File 'lib/swarm_sdk/ruby_llm_patches/responses_api_patch.rb', line 25

def response_id
  @response_id
end

Class Method Details

.from_h(hash) ⇒ Object

rubocop:disable Style/ClassMethodsDefinitions



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/swarm_sdk/ruby_llm_patches/responses_api_patch.rb', line 79

def self.from_h(hash) # rubocop:disable Style/ClassMethodsDefinitions
  hash = hash.transform_keys(&:to_sym)
  last_activity = hash[:last_activity] ? Time.parse(hash[:last_activity]) : nil

  new(
    response_id: hash[:response_id],
    last_activity: last_activity,
    failure_count: hash[:failure_count] || 0,
    disabled: hash[:disabled] || false,
  )
end

Instance Method Details

#disabled?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/swarm_sdk/ruby_llm_patches/responses_api_patch.rb', line 66

def disabled?
  @disabled
end

#record_failure!Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/swarm_sdk/ruby_llm_patches/responses_api_patch.rb', line 55

def record_failure!
  @failure_count += 1

  if @failure_count >= MAX_FAILURES
    @disabled = true
  else
    @response_id = nil
    @last_activity = nil
  end
end

#reset!Object



34
35
36
37
38
39
# File 'lib/swarm_sdk/ruby_llm_patches/responses_api_patch.rb', line 34

def reset!
  @response_id = nil
  @last_activity = nil
  @failure_count = 0
  @disabled = false
end

#to_hObject



70
71
72
73
74
75
76
77
# File 'lib/swarm_sdk/ruby_llm_patches/responses_api_patch.rb', line 70

def to_h
  {
    response_id: @response_id,
    last_activity: @last_activity&.iso8601,
    failure_count: @failure_count,
    disabled: @disabled,
  }
end

#update(new_response_id) ⇒ Object



41
42
43
44
45
# File 'lib/swarm_sdk/ruby_llm_patches/responses_api_patch.rb', line 41

def update(new_response_id)
  @response_id = new_response_id
  @last_activity = Time.now
  @failure_count = 0
end

#valid?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# File 'lib/swarm_sdk/ruby_llm_patches/responses_api_patch.rb', line 47

def valid?
  return false if @disabled
  return false unless @response_id
  return false unless @last_activity

  (Time.now - @last_activity) < RESPONSE_ID_TTL
end