Class: RubyCoded::Chat::CodexBridge

Inherits:
Object
  • Object
show all
Includes:
ErrorHandling, RequestBuilder, SSEParser, TokenManager, ToolHandling
Defined in:
lib/ruby_coded/chat/codex_bridge.rb,
lib/ruby_coded/chat/codex_bridge/sse_parser.rb,
lib/ruby_coded/chat/codex_bridge/token_manager.rb,
lib/ruby_coded/chat/codex_bridge/tool_approval.rb,
lib/ruby_coded/chat/codex_bridge/tool_handling.rb,
lib/ruby_coded/chat/codex_bridge/error_handling.rb,
lib/ruby_coded/chat/codex_bridge/request_builder.rb

Overview

HTTP client for the ChatGPT Codex backend (Responses API). Implements the same public interface as LLMBridge so App can swap between them based on the active auth_method.

Defined Under Namespace

Modules: ErrorHandling, RequestBuilder, SSEParser, TokenManager, ToolApproval, ToolHandling

Constant Summary collapse

CODEX_BASE_URL =
"https://chatgpt.com"
CODEX_RESPONSES_PATH =
"/backend-api/codex/responses"
DEFAULT_MODEL =
"gpt-5.4"
MAX_RATE_LIMIT_RETRIES =
2
RATE_LIMIT_BASE_DELAY =
2
MAX_WRITE_TOOL_ROUNDS =
50
MAX_TOTAL_TOOL_ROUNDS =
200
TOOL_ROUNDS_WARNING_THRESHOLD =
0.8
MAX_TOOL_RESULT_CHARS =
10_000

Constants included from ErrorHandling

ErrorHandling::AGENT_SWITCH_PATTERN

Constants included from TokenManager

TokenManager::TOKEN_REFRESH_BUFFER

Constants included from RequestBuilder

RequestBuilder::CODEX_HEADERS_BASE, RequestBuilder::DEFAULT_INSTRUCTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state, credentials_store:, auth_manager:, project_root: Dir.pwd) ⇒ CodexBridge

Returns a new instance of CodexBridge.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ruby_coded/chat/codex_bridge.rb', line 54

def initialize(state, credentials_store:, auth_manager:, project_root: Dir.pwd)
  @state = state
  @credentials_store = credentials_store
  @auth_manager = auth_manager
  @project_root = project_root
  @cancel_requested = @agentic_mode = @plan_mode = false
  @model = state.model
  @conversation_history = []
  @tool_registry = Tools::Registry.new(project_root: @project_root)
  reset_call_counts
  @conn = build_connection
end

Instance Attribute Details

#agentic_modeObject (readonly)

Returns the value of attribute agentic_mode.



52
53
54
# File 'lib/ruby_coded/chat/codex_bridge.rb', line 52

def agentic_mode
  @agentic_mode
end

#plan_modeObject (readonly)

Returns the value of attribute plan_mode.



52
53
54
# File 'lib/ruby_coded/chat/codex_bridge.rb', line 52

def plan_mode
  @plan_mode
end

#project_rootObject (readonly)

Returns the value of attribute project_root.



52
53
54
# File 'lib/ruby_coded/chat/codex_bridge.rb', line 52

def project_root
  @project_root
end

Instance Method Details

#approve_all_tools!Object



110
111
112
113
# File 'lib/ruby_coded/chat/codex_bridge.rb', line 110

def approve_all_tools!
  @state.enable_auto_approve!
  @state.tool_confirmation_response = :approved
end

#approve_tool!Object



106
107
108
# File 'lib/ruby_coded/chat/codex_bridge.rb', line 106

def approve_tool!
  @state.tool_confirmation_response = :approved
end

#cancel!Object



77
78
79
80
# File 'lib/ruby_coded/chat/codex_bridge.rb', line 77

def cancel!
  @cancel_requested = true
  @state.mutex.synchronize { @state.tool_cv.signal }
end

#reject_tool!Object



115
116
117
# File 'lib/ruby_coded/chat/codex_bridge.rb', line 115

def reject_tool!
  @state.tool_confirmation_response = :rejected
end

#reset_agent_session!Object



119
120
121
122
123
# File 'lib/ruby_coded/chat/codex_bridge.rb', line 119

def reset_agent_session!
  @tool_call_count = 0
  @write_tool_call_count = 0
  @conversation_history = []
end

#reset_chat!(model_name) ⇒ Object



82
83
84
85
# File 'lib/ruby_coded/chat/codex_bridge.rb', line 82

def reset_chat!(model_name)
  @model = model_name
  @conversation_history = []
end

#send_async(input) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/ruby_coded/chat/codex_bridge.rb', line 67

def send_async(input)
  prepare_send(input)
  @conversation_history << { role: "user", content: input }
  Thread.new do
    attempt_with_retries(input)
  ensure
    @state.streaming = false
  end
end

#toggle_agentic_mode!(enabled) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/ruby_coded/chat/codex_bridge.rb', line 87

def toggle_agentic_mode!(enabled)
  @agentic_mode = enabled
  @state.agentic_mode = enabled
  if enabled && @plan_mode
    @plan_mode = false
    @state.deactivate_plan_mode!
  end
  @state.disable_auto_approve! unless enabled
end

#toggle_plan_mode!(enabled) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/ruby_coded/chat/codex_bridge.rb', line 97

def toggle_plan_mode!(enabled)
  @plan_mode = enabled
  return unless enabled && @agentic_mode

  @agentic_mode = false
  @state.agentic_mode = false
  @state.disable_auto_approve!
end