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:, base_url: ANTHROPIC_API_URL, logger: nil) ⇒ TextTransport

Returns a new instance of TextTransport.

Parameters:

  • base_url (String) (defaults to: ANTHROPIC_API_URL)

    Anthropic Messages API URL

  • api_key (String)

    Anthropic API key

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

    optional logger



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

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

Instance Method Details

#chat(messages:, tools: nil, stream: false, max_tokens: nil, temperature: nil, model: nil, on_chat_chunk: nil, observer: nil) {|Hash| ... } ⇒ Response

Send a multi-turn chat completion request via the Anthropic Messages API.

Parameters:

  • messages (Array<Hash>)

    conversation messages with :role and :content

  • tools (Array<Hash>, nil) (defaults to: nil)

    tool definitions (Anthropic tool format)

  • stream (Boolean) (defaults to: false)

    whether to stream the response

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

    maximum tokens in the response

  • temperature (Float, nil) (defaults to: nil)

    sampling temperature

Yields:

  • (Hash)

    streaming chunks when stream: true

Returns:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/agent_harness/text_transport.rb', line 46

def chat(messages:, tools: nil, stream: false, max_tokens: nil, temperature: nil,
  model: nil, on_chat_chunk: nil, observer: nil, &on_chunk)
  model ||= DEFAULT_MODEL
  timeout = DEFAULT_TIMEOUT
  max_tokens ||= DEFAULT_MAX_TOKENS

  uri = URI(@base_url)

  system_messages = messages.select { |m| m[:role] == "system" || m["role"] == "system" }
  non_system = messages.reject { |m| m[:role] == "system" || m["role"] == "system" }
  has_stream_receiver = on_chunk || on_chat_chunk || observer_responds_to?(observer, :on_chat_chunk)
  request_stream = stream && has_stream_receiver

  body = build_chat_request_body(
    model: model,
    max_tokens: max_tokens,
    messages: non_system,
    system_messages: system_messages,
    tools: tools,
    temperature: temperature,
    stream: request_stream
  )

  start_time = Time.now

  if request_stream
    combined = build_chat_chunk_callback(on_chunk, on_chat_chunk, observer)
    result = make_streaming_request(uri, body, timeout: timeout, &combined)
    duration = Time.now - start_time
    build_streaming_response(result, duration: duration, model: model)
  else
    http_response = make_request(uri, body, timeout: timeout)
    duration = Time.now - start_time
    parse_response(http_response, duration: duration, model: model)
  end
end

#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:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/agent_harness/text_transport.rb', line 94

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

  uri = URI(@base_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