Class: AgentHarness::TextTransport

Inherits:
Object
  • Object
show all
Defined in:
lib/agent_harness/text_transport.rb

Overview

Direct HTTP transport for text-only provider interactions.

Bypasses the CLI entirely by calling the provider’s REST API directly. Currently supports Anthropic’s Messages API. This transport is used when callers declare a task as text-only via mode: :text on send_message.

The transport preserves the same Response structure, token tracking, and error classification semantics as the CLI path so that callers do not need to distinguish between transport modes after the call.

Examples:

transport = AgentHarness::TextTransport.new(api_key: "sk-ant-...")
response = transport.send_message("Summarize this PR", model: "claude-sonnet-4-20250514")

Constant Summary collapse

ANTHROPIC_API_URL =
"https://api.anthropic.com/v1/messages"
ANTHROPIC_API_VERSION =
"2023-06-01"
DEFAULT_MODEL =
"claude-sonnet-4-20250514"
DEFAULT_MAX_TOKENS =
4096
DEFAULT_TIMEOUT =
300

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, logger: nil) ⇒ TextTransport

Returns a new instance of TextTransport.

Parameters:

  • api_key (String)

    Anthropic API key

  • logger (Logger, nil) (defaults to: nil)

    optional logger



30
31
32
33
# File 'lib/agent_harness/text_transport.rb', line 30

def initialize(api_key:, logger: nil)
  @api_key = api_key
  @logger = logger
end

Instance Method Details

#send_message(prompt, model: nil, timeout: nil, max_tokens: nil) ⇒ Response

Send a text-only message via the Anthropic Messages API.

Parameters:

  • prompt (String)

    the user prompt

  • model (String, nil) (defaults to: nil)

    model to use (defaults to DEFAULT_MODEL)

  • timeout (Integer, nil) (defaults to: nil)

    request timeout in seconds

  • max_tokens (Integer, nil) (defaults to: nil)

    maximum tokens in the response

Returns:

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/agent_harness/text_transport.rb', line 46

def send_message(prompt, model: nil, timeout: nil, max_tokens: nil)
  model ||= DEFAULT_MODEL
  timeout ||= DEFAULT_TIMEOUT
  max_tokens ||= DEFAULT_MAX_TOKENS

  uri = URI(ANTHROPIC_API_URL)
  body = {
    model: model,
    max_tokens: max_tokens,
    messages: [{role: "user", content: prompt}]
  }

  start_time = Time.now
  http_response = make_request(uri, body, timeout: timeout)
  duration = Time.now - start_time

  parse_response(http_response, duration: duration, model: model)
end