Class: LlmGateway::Clients::OpenAI::TokenManager

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_gateway/clients/openai_codex/token_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token: nil, refresh_token:, expires_at: nil, account_id: nil, client_id: OAuthFlow::CLIENT_ID) ⇒ TokenManager

Returns a new instance of TokenManager.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/llm_gateway/clients/openai_codex/token_manager.rb', line 14

def initialize(
  access_token: nil,
  refresh_token:,
  expires_at: nil,
  account_id: nil,
  client_id: OAuthFlow::CLIENT_ID
)
  @access_token  = access_token
  @refresh_token = refresh_token
  @expires_at    = parse_expires_at(expires_at)
  @account_id    = 
  @client_id     = client_id
  @on_token_refresh = nil
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



11
12
13
# File 'lib/llm_gateway/clients/openai_codex/token_manager.rb', line 11

def access_token
  @access_token
end

#account_idObject (readonly)

Returns the value of attribute account_id.



11
12
13
# File 'lib/llm_gateway/clients/openai_codex/token_manager.rb', line 11

def 
  @account_id
end

#client_idObject (readonly)

Returns the value of attribute client_id.



11
12
13
# File 'lib/llm_gateway/clients/openai_codex/token_manager.rb', line 11

def client_id
  @client_id
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



11
12
13
# File 'lib/llm_gateway/clients/openai_codex/token_manager.rb', line 11

def expires_at
  @expires_at
end

#on_token_refreshObject

Returns the value of attribute on_token_refresh.



12
13
14
# File 'lib/llm_gateway/clients/openai_codex/token_manager.rb', line 12

def on_token_refresh
  @on_token_refresh
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



11
12
13
# File 'lib/llm_gateway/clients/openai_codex/token_manager.rb', line 11

def refresh_token
  @refresh_token
end

Instance Method Details

#ensure_valid_tokenObject



35
36
37
# File 'lib/llm_gateway/clients/openai_codex/token_manager.rb', line 35

def ensure_valid_token
  refresh_access_token! if token_expired?
end

#refresh_access_token!Object

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/llm_gateway/clients/openai_codex/token_manager.rb', line 39

def refresh_access_token!
  raise ArgumentError, "Cannot refresh token: refresh_token not provided" unless @refresh_token

  result = OAuthFlow.refresh_access_token(@refresh_token, client_id: @client_id)

  @access_token  = result[:access_token]
  @refresh_token = result[:refresh_token]
  @expires_at    = result[:expires_at]
  @account_id    = result[:account_id] if result[:account_id]

  @on_token_refresh&.call(@access_token, @refresh_token, @expires_at)

  @access_token
end

#token_expired?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/llm_gateway/clients/openai_codex/token_manager.rb', line 29

def token_expired?
  return true if @expires_at.nil?

  Time.now >= @expires_at
end