Class: AgentHarness::Providers::Codex

Inherits:
Base
  • Object
show all
Defined in:
lib/agent_harness/providers/codex.rb

Overview

OpenAI Codex CLI provider

Provides integration with the OpenAI Codex CLI tool.

Instance Attribute Summary

Attributes inherited from Base

#config, #executor, #logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#configure, #initialize, #send_message

Methods included from Adapter

#auth_type, #build_mcp_flags, #dangerous_mode_flags, #fetch_mcp_servers, included, #send_message, #supported_mcp_transports, #supports_dangerous_mode?, #supports_mcp?, #validate_mcp_servers!

Constructor Details

This class inherits a constructor from AgentHarness::Providers::Base

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


20
21
22
23
# File 'lib/agent_harness/providers/codex.rb', line 20

def available?
  executor = AgentHarness.configuration.command_executor
  !!executor.which(binary_name)
end

.binary_nameObject



16
17
18
# File 'lib/agent_harness/providers/codex.rb', line 16

def binary_name
  "codex"
end

.discover_modelsObject



45
46
47
48
49
50
51
# File 'lib/agent_harness/providers/codex.rb', line 45

def discover_models
  return [] unless available?

  [
    {name: "codex", family: "codex", tier: "standard", provider: "codex"}
  ]
end

.firewall_requirementsObject



25
26
27
28
29
30
31
32
33
# File 'lib/agent_harness/providers/codex.rb', line 25

def firewall_requirements
  {
    domains: [
      "api.openai.com",
      "openai.com"
    ],
    ip_ranges: []
  }
end

.instruction_file_pathsObject



35
36
37
38
39
40
41
42
43
# File 'lib/agent_harness/providers/codex.rb', line 35

def instruction_file_paths
  [
    {
      path: "AGENTS.md",
      description: "OpenAI Codex agent instructions",
      symlink: false
    }
  ]
end

.provider_nameObject



12
13
14
# File 'lib/agent_harness/providers/codex.rb', line 12

def provider_name
  :codex
end

Instance Method Details

#auth_statusObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/agent_harness/providers/codex.rb', line 112

def auth_status
  api_key = ENV["OPENAI_API_KEY"]
  if api_key && !api_key.strip.empty?
    if api_key.strip.start_with?("sk-")
      return {valid: true, expires_at: nil, error: nil, auth_method: :api_key}
    else
      return {valid: false, expires_at: nil, error: "OPENAI_API_KEY is set but does not appear to be a valid OpenAI API key"}
    end
  end

  credentials = read_codex_credentials
  if credentials
    key = credentials["api_key"] || credentials["apiKey"] || credentials["OPENAI_API_KEY"]
    if key.is_a?(String) && !key.strip.empty?
      if key.strip.start_with?("sk-")
        return {valid: true, expires_at: nil, error: nil, auth_method: :config_file}
      else
        return {valid: false, expires_at: nil, error: "Config file API key is set but does not appear to be a valid OpenAI API key"}
      end
    end
  end

  {valid: false, expires_at: nil, error: "No OpenAI API key found. Set OPENAI_API_KEY or configure in #{codex_config_path}"}
rescue IOError, JSON::ParserError => e
  {valid: false, expires_at: nil, error: e.message}
end

#capabilitiesObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/agent_harness/providers/codex.rb', line 62

def capabilities
  {
    streaming: false,
    file_upload: false,
    vision: false,
    tool_use: true,
    json_mode: false,
    mcp: false,
    dangerous_mode: false
  }
end

#display_nameObject



58
59
60
# File 'lib/agent_harness/providers/codex.rb', line 58

def display_name
  "OpenAI Codex CLI"
end

#error_patternsObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/agent_harness/providers/codex.rb', line 83

def error_patterns
  {
    rate_limited: [
      /rate.?limit/i,
      /too.?many.?requests/i,
      /429/
    ],
    auth_expired: [
      /invalid.*api.*key/i,
      /unauthorized/i,
      /authentication/i,
      /401/,
      /incorrect.*api.*key/i
    ],
    quota_exceeded: [
      /quota.*exceeded/i,
      /insufficient.*quota/i,
      /billing/i
    ],
    transient: [
      /timeout/i,
      /connection.*reset/i,
      /service.*unavailable/i,
      /503/,
      /502/
    ]
  }
end

#health_statusObject



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/agent_harness/providers/codex.rb', line 139

def health_status
  unless self.class.available?
    return {healthy: false, message: "Codex CLI not found in PATH. Install from https://github.com/openai/codex"}
  end

  auth = auth_status
  unless auth[:valid]
    return {healthy: false, message: auth[:error]}
  end

  {healthy: true, message: "Codex CLI available and authenticated"}
end

#nameObject



54
55
56
# File 'lib/agent_harness/providers/codex.rb', line 54

def name
  "codex"
end

#session_flags(session_id) ⇒ Object



78
79
80
81
# File 'lib/agent_harness/providers/codex.rb', line 78

def session_flags(session_id)
  return [] unless session_id && !session_id.empty?
  ["--session", session_id]
end

#supports_sessions?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/agent_harness/providers/codex.rb', line 74

def supports_sessions?
  true
end

#validate_configObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/agent_harness/providers/codex.rb', line 152

def validate_config
  errors = []

  flags = @config.default_flags
  unless flags.nil?
    if flags.is_a?(Array)
      invalid = flags.reject { |f| f.is_a?(String) }
      errors << "default_flags contains non-string values" if invalid.any?
    else
      errors << "default_flags must be an array of strings"
    end
  end

  {valid: errors.empty?, errors: errors}
end