Class: FetchHive::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/fetch_hive/client.rb

Overview

Idiomatic facade over the OpenAPI-generated code.

Usage:

client = FetchHive::Client.new(api_key: ENV["FETCH_HIVE_API_KEY"])

# Non-streaming prompt
result = client.invoke_prompt(deployment: "my-prompt", inputs: { name: "Alice" })
puts result["response"]

# Streaming agent
client.invoke_agent_stream(agent: "my-agent", message: "Hello") do |chunk|
  case chunk["type"]
  when "response" then print chunk["response"]
  when "tool"     then puts "\nCalling tool: #{chunk['tool']}"
  when "usage"    then puts "\nUsage: #{chunk['usage']}"
  end
end

Constant Summary collapse

DEFAULT_BASE_URL =
"https://api.fetchhive.com/v1"

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, base_url: DEFAULT_BASE_URL, timeout: 120) ⇒ Client

Returns a new instance of Client.

Parameters:

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

    Bearer token. Falls back to FETCH_HIVE_API_KEY env var.

  • base_url (String) (defaults to: DEFAULT_BASE_URL)

    Override the API base URL.

  • timeout (Integer) (defaults to: 120)

    Request timeout in seconds (default: 120).

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
# File 'lib/fetch_hive/client.rb', line 33

def initialize(api_key: nil, base_url: DEFAULT_BASE_URL, timeout: 120)
  resolved = api_key || ENV["FETCH_HIVE_API_KEY"]
  raise ArgumentError, "api_key is required. Pass it explicitly or set FETCH_HIVE_API_KEY." if resolved.nil? || resolved.empty?

  @api_key  = resolved
  @base_url = base_url.to_s.sub(%r{/+\z}, "")
  @timeout  = timeout
end

Instance Method Details

#invoke_agent(agent:, message:, thread_id: nil, user: nil, messages: nil, image_urls: nil) ⇒ Object

Send a message to an agent and return the full response hash.



81
82
83
84
85
86
87
88
# File 'lib/fetch_hive/client.rb', line 81

def invoke_agent(agent:, message:, thread_id: nil, user: nil, messages: nil, image_urls: nil)
  body = { agent: agent, message: message, streaming: false }
  body[:thread_id]  = thread_id  if thread_id
  body[:user]       = user       if user
  body[:messages]   = messages   if messages
  body[:image_urls] = image_urls if image_urls
  post("/agent/invoke", body)
end

#invoke_agent_stream(agent:, message:, thread_id: nil, user: nil, messages: nil, image_urls: nil, &block) ⇒ Object

Send a message to an agent and stream SSE events. Yields each parsed event hash. Returns an Enumerator when no block given.



92
93
94
95
96
97
98
99
# File 'lib/fetch_hive/client.rb', line 92

def invoke_agent_stream(agent:, message:, thread_id: nil, user: nil, messages: nil, image_urls: nil, &block)
  body = { agent: agent, message: message, streaming: true }
  body[:thread_id]  = thread_id  if thread_id
  body[:user]       = user       if user
  body[:messages]   = messages   if messages
  body[:image_urls] = image_urls if image_urls
  post_stream("/agent/invoke", body, &block)
end

#invoke_prompt(deployment:, variant: nil, inputs: nil, user: nil) ⇒ Object

Invoke a prompt deployment and return the full response hash.



45
46
47
48
49
50
51
# File 'lib/fetch_hive/client.rb', line 45

def invoke_prompt(deployment:, variant: nil, inputs: nil, user: nil)
  body = { deployment: deployment, streaming: false }
  body[:variant] = variant if variant
  body[:inputs]  = inputs  if inputs
  body[:user]    = user    if user
  post("/invoke", body)
end

#invoke_prompt_stream(deployment:, variant: nil, inputs: nil, user: nil, &block) ⇒ Object

Invoke a prompt deployment and stream SSE events. Yields each parsed event hash. Returns an Enumerator when no block given.



55
56
57
58
59
60
61
# File 'lib/fetch_hive/client.rb', line 55

def invoke_prompt_stream(deployment:, variant: nil, inputs: nil, user: nil, &block)
  body = { deployment: deployment, streaming: true }
  body[:variant] = variant if variant
  body[:inputs]  = inputs  if inputs
  body[:user]    = user    if user
  post_stream("/invoke", body, &block)
end

#invoke_workflow(deployment:, variant: nil, inputs: nil, async_mode: false, callback_url: nil, user: nil) ⇒ Object

Invoke a workflow deployment (sync or async).



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fetch_hive/client.rb', line 66

def invoke_workflow(deployment:, variant: nil, inputs: nil, async_mode: false, callback_url: nil, user: nil)
  body = { deployment: deployment }
  body[:variant] = variant if variant
  body[:inputs]  = inputs  if inputs
  body[:user]    = user    if user
  if async_mode
    body[:async] = { enabled: true }
    body[:async][:callback_url] = callback_url if callback_url
  end
  post("/workflow/invoke", body)
end