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|
  print chunk["content"] if chunk["type"] == "delta"
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)


29
30
31
32
33
34
35
36
# File 'lib/fetch_hive/client.rb', line 29

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.



77
78
79
80
81
82
83
84
# File 'lib/fetch_hive/client.rb', line 77

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.



88
89
90
91
92
93
94
95
# File 'lib/fetch_hive/client.rb', line 88

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.



41
42
43
44
45
46
47
# File 'lib/fetch_hive/client.rb', line 41

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.



51
52
53
54
55
56
57
# File 'lib/fetch_hive/client.rb', line 51

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).



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

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