Class: AgentHarness::Providers::GithubCopilot

Inherits:
Base
  • Object
show all
Includes:
TokenUsageParsing
Defined in:
lib/agent_harness/providers/github_copilot.rb

Constant Summary collapse

MODEL_PATTERN =
/^gpt-[\d.o-]+(?:-turbo)?(?:-mini)?$/i
JSON_OUTPUT_MIN_VERSION =
Gem::Version.new("0.0.422").freeze
SUBCOMMAND_CLI_MIN_VERSION =
Gem::Version.new("0.1.0").freeze
UNSUPPORTED_SUBCOMMAND_CLI_MESSAGE =
"github-copilot-cli 0.1.x does not expose a non-interactive send interface; " \
"the what-the-shell subcommand is interactive and cannot be used by AgentHarness."
SMOKE_TEST_CONTRACT =
{
  prompt: "Reply with exactly OK.",
  expected_output: "OK",
  timeout: 30,
  require_output: true,
  success_message: "Smoke test passed"
}.freeze

Constants inherited from Base

Base::COMMON_ERROR_PATTERNS, Base::DEFAULT_SMOKE_TEST_CONTRACT

Instance Attribute Summary

Attributes inherited from Base

#config, #executor, #logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#api_key_env_var_names, #api_key_unset_vars, #cli_env_overrides, #configure, #initialize, #parse_rate_limit_reset, #parse_test_error, #sandboxed_environment?, #subscription_unset_vars, #test_command_overrides

Methods included from Adapter

#auth_lock_config, #build_mcp_flags, #config_file_content, #error_classification_patterns, #fetch_mcp_servers, #health_status, included, metadata_package_name, #noisy_error_patterns, normalize_metadata_installation, normalize_metadata_source_type, normalize_metadata_version_requirement, #notify_hook_content, #parse_rate_limit_reset, #smoke_test, #smoke_test_contract, #supported_mcp_transports, #supports_dangerous_mode?, #supports_mcp?, #supports_text_mode?, #supports_tool_control?, #token_usage_from_api_response, #validate_config, #validate_mcp_servers!

Constructor Details

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

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


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

def available?
  executor = AgentHarness.configuration.command_executor
  return false unless executor.which(binary_name)

  !subcommand_cli_version?(copilot_cli_version(executor: executor))
rescue
  false
end

.binary_nameObject



31
32
33
# File 'lib/agent_harness/providers/github_copilot.rb', line 31

def binary_name
  "github-copilot-cli"
end

.discover_modelsObject



90
91
92
93
94
95
96
97
98
# File 'lib/agent_harness/providers/github_copilot.rb', line 90

def discover_models
  return [] unless available?

  [
    {name: "gpt-4o", family: "gpt-4o", tier: "standard", provider: "github_copilot"},
    {name: "gpt-4o-mini", family: "gpt-4o-mini", tier: "mini", provider: "github_copilot"},
    {name: "gpt-4-turbo", family: "gpt-4-turbo", tier: "advanced", provider: "github_copilot"}
  ]
end

.firewall_requirementsObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/agent_harness/providers/github_copilot.rb', line 67

def firewall_requirements
  {
    domains: [
      "copilot-proxy.githubusercontent.com",
      "api.githubcopilot.com",
      "copilot-telemetry.githubusercontent.com",
      "default.exp-tas.com",
      "copilot-completions.githubusercontent.com"
    ],
    ip_ranges: []
  }
end

.install_command(version: nil) ⇒ Object



51
52
53
# File 'lib/agent_harness/providers/github_copilot.rb', line 51

def install_command(version: nil)
  installation_contract(version: version)&.fetch(:install_command)
end

.installation_contract(version: nil) ⇒ Object



44
45
46
47
48
49
# File 'lib/agent_harness/providers/github_copilot.rb', line 44

def installation_contract(version: nil)
  # The published @githubnext/github-copilot-cli package only has
  # 0.1.x releases, and those expose an interactive subcommand instead
  # of the non-interactive -p prompt path AgentHarness uses.
  nil
end

.instruction_file_pathsObject



80
81
82
83
84
85
86
87
88
# File 'lib/agent_harness/providers/github_copilot.rb', line 80

def instruction_file_paths
  [
    {
      path: ".github/copilot-instructions.md",
      description: "GitHub Copilot agent instructions",
      symlink: true
    }
  ]
end

.model_family(provider_model_name) ⇒ Object



104
105
106
# File 'lib/agent_harness/providers/github_copilot.rb', line 104

def model_family(provider_model_name)
  provider_model_name
end

.provider_metadata_overridesObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/agent_harness/providers/github_copilot.rb', line 55

def 
  {
    auth: {
      service: :github,
      api_family: :github_copilot
    },
    identity: {
      bot_usernames: ["github-copilot[bot]"]
    }
  }
end

.provider_model_name(family_name) ⇒ Object



108
109
110
# File 'lib/agent_harness/providers/github_copilot.rb', line 108

def provider_model_name(family_name)
  family_name
end

.provider_nameObject



27
28
29
# File 'lib/agent_harness/providers/github_copilot.rb', line 27

def provider_name
  :github_copilot
end

.smoke_test_contractObject



100
101
102
# File 'lib/agent_harness/providers/github_copilot.rb', line 100

def smoke_test_contract
  SMOKE_TEST_CONTRACT
end

.supports_model_family?(family_name) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/agent_harness/providers/github_copilot.rb', line 112

def supports_model_family?(family_name)
  MODEL_PATTERN.match?(family_name)
end

Instance Method Details

#auth_typeObject



197
198
199
# File 'lib/agent_harness/providers/github_copilot.rb', line 197

def auth_type
  :oauth
end

#capabilitiesObject



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/agent_harness/providers/github_copilot.rb', line 166

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

#configuration_schemaObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/agent_harness/providers/github_copilot.rb', line 149

def configuration_schema
  {
    fields: [
      {
        name: :model,
        type: :string,
        label: "Model",
        required: false,
        hint: "Copilot model identifier (for example gpt-4o or gpt-4o-mini)",
        accepts_arbitrary: true
      }
    ],
    auth_modes: [:oauth],
    openai_compatible: false
  }
end

#dangerous_mode_flags(probe_timeout: nil, env: {}, version: nil) ⇒ Object



178
179
180
181
182
183
184
# File 'lib/agent_harness/providers/github_copilot.rb', line 178

def dangerous_mode_flags(probe_timeout: nil, env: {}, version: nil)
  version ||= copilot_cli_version(probe_timeout: probe_timeout, env: env)
  return [] if subcommand_cli_version?(version)
  return [] unless supports_json_output_format?(version: version)

  ["--allow-all"]
end

#display_nameObject



145
146
147
# File 'lib/agent_harness/providers/github_copilot.rb', line 145

def display_name
  "GitHub Copilot CLI"
end

#error_patternsObject



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/agent_harness/providers/github_copilot.rb', line 216

def error_patterns
  {
    auth_expired: [
      /not.?authorized/i,
      /access.?denied/i,
      /permission.?denied/i,
      /not.?enabled/i,
      /subscription.?required/i
    ],
    rate_limited: [
      /usage.?limit/i,
      /rate.?limit/i
    ],
    transient: [
      /connection.?error/i,
      /timeout/i,
      /try.?again/i
    ],
    permanent: [
      /invalid.?command/i,
      /unknown.?flag/i
    ]
  }
end

#execution_semanticsObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/agent_harness/providers/github_copilot.rb', line 201

def execution_semantics
  {
    prompt_delivery: :arg,
    # Older Copilot CLIs fall back to plain-text prompt mode, so metadata
    # must not claim JSON-only output even though newer versions support it.
    output_format: :text,
    sandbox_aware: false,
    uses_subcommand: false,
    non_interactive_flag: nil,
    legitimate_exit_codes: [0],
    stderr_is_diagnostic: true,
    parses_rate_limit_reset: false
  }
end

#nameObject



141
142
143
# File 'lib/agent_harness/providers/github_copilot.rb', line 141

def name
  "github_copilot"
end

#send_message(prompt:, **options) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/agent_harness/providers/github_copilot.rb', line 252

def send_message(prompt:, **options)
  log_debug("send_message_start", prompt_length: prompt.length, options: options.keys)

  options = normalize_provider_runtime(options)
  options = normalize_mcp_servers(options)
  validate_mcp_servers!(options[:mcp_servers]) if options[:mcp_servers]&.any?

  timeout = options[:timeout] || @config.timeout || default_timeout
  raise TimeoutError, "Command timed out before execution started" if timeout <= 0

  env = build_env(options)
  options = options.merge(_version_probe_timeout: [timeout, 5].min, _command_env: env)

  start_time = Time.now
  command = build_command(prompt, options)
  preparation = build_execution_preparation(options)
  remaining_timeout = timeout - (Time.now - start_time)
  raise TimeoutError, "Command timed out before execution started" if remaining_timeout <= 0

  json_output_requested = command.include?("--output-format") && command.include?("json")

  result = execute_with_timeout(
    command,
    timeout: remaining_timeout,
    env: env,
    preparation: preparation,
    **command_execution_options(options)
  )
  duration = Time.now - start_time

  response = parse_response(result, duration: duration, json_output_requested: json_output_requested)
  runtime = options[:provider_runtime]
  effective_runtime_model = normalized_model_name(runtime&.model)
  if effective_runtime_model
    response = Response.new(
      output: response.output,
      exit_code: response.exit_code,
      duration: response.duration,
      provider: response.provider,
      model: effective_runtime_model,
      tokens: response.tokens,
      metadata: response.,
      error: response.error
    )
  end

  track_tokens(response) if response.tokens

  log_debug("send_message_complete", duration: duration, tokens: response.tokens)

  response
rescue McpConfigurationError, McpUnsupportedError, McpTransportUnsupportedError
  raise
rescue => e
  handle_error(e, prompt: prompt, options: options)
end

#session_flags(session_id, version: nil, probe_timeout: nil, env: {}) ⇒ Object



190
191
192
193
194
195
# File 'lib/agent_harness/providers/github_copilot.rb', line 190

def session_flags(session_id, version: nil, probe_timeout: nil, env: {})
  return [] unless session_id && !session_id.empty?
  return [] unless legacy_prompt_cli?(version: version, probe_timeout: probe_timeout, env: env)

  ["--resume", session_id]
end

#supports_sessions?(probe_timeout: nil, env: {}, version: nil) ⇒ Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/agent_harness/providers/github_copilot.rb', line 186

def supports_sessions?(probe_timeout: nil, env: {}, version: nil)
  legacy_prompt_cli?(version: version, probe_timeout: probe_timeout, env: env)
end

#supports_token_counting?Boolean

Returns:

  • (Boolean)


248
249
250
# File 'lib/agent_harness/providers/github_copilot.rb', line 248

def supports_token_counting?
  supports_json_output_format?
end

#translate_error(message) ⇒ Object



241
242
243
244
245
246
# File 'lib/agent_harness/providers/github_copilot.rb', line 241

def translate_error(message)
  case message
  when /github-copilot-cli.*not found/i then "GitHub Copilot CLI not installed."
  else message
  end
end