Class: RubyConversations::Client

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

Overview

HTTP client for interacting with the conversations API

Constant Summary collapse

PROMPT_ATTRIBUTES =
%w[
  id name message role temperature valid_placeholders created_at updated_at latest_version_id llm
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, jwt_secret:) ⇒ Client

Initialize a new API client

Parameters:

  • url (String)

    The base URL for the API

  • jwt_secret (String, #call)

    The JWT secret or a callable that returns a JWT token



19
20
21
22
23
24
25
26
# File 'lib/ruby_conversations/client.rb', line 19

def initialize(url:, jwt_secret:)
  @url = url
  @jwt_secret = jwt_secret
  @jwt_token = nil
  @jwt_expiration = nil
  @logger = Logger.new($stdout)
  @client = build_client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



10
11
12
# File 'lib/ruby_conversations/client.rb', line 10

def client
  @client
end

Instance Method Details

#fetch_all_conversation_templatesArray<Hash>

Fetch all available conversation templates

Returns:

  • (Array<Hash>)

    Array of template data including messages and prompts



73
74
75
76
# File 'lib/ruby_conversations/client.rb', line 73

def fetch_all_conversation_templates
  response = client.get('api/ai_conversations')
  handle_response(response)
end

#fetch_conversation_template(template_name) ⇒ Hash

Fetch a conversation template by name

Parameters:

  • template_name (String)

    The name of the template to fetch

Returns:

  • (Hash)

    The template data including messages and prompts



66
67
68
69
# File 'lib/ruby_conversations/client.rb', line 66

def fetch_conversation_template(template_name)
  response = client.get("api/ai_conversations/#{template_name}")
  handle_response(response)
end

#fetch_prompt(name) ⇒ Hash

Fetch a prompt by name

Parameters:

  • name (String)

    The name of the prompt to fetch

Returns:

  • (Hash)

    The prompt attributes



81
82
83
84
85
# File 'lib/ruby_conversations/client.rb', line 81

def fetch_prompt(name)
  response = client.get("api/prompts/#{name}")
  data = handle_response(response)
  map_prompt_attributes(data)
end

#store_conversation(conversation) ⇒ Hash

Store a new conversation

Parameters:

  • conversation (Conversation)

    The conversation to store

Returns:

  • (Hash)

    The API response data

Raises:

  • (Error)

    If the API response is missing a conversation ID



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

def store_conversation(conversation)
  response = client.post('api/ai_conversations', conversation.conversation_attributes_for_storage)
  data = handle_response(response)
  raise RubyConversations::Error, 'API response missing conversation ID' unless data['id'].present?

  conversation.id = data['id']
  data
end

#store_conversation_or_message(conversation, message) ⇒ Hash

Store a conversation or add a message to an existing conversation

Parameters:

  • conversation (Conversation)

    The conversation to store

  • message (Message)

    The message to store

Returns:

  • (Hash)

    The API response data



32
33
34
35
36
37
38
# File 'lib/ruby_conversations/client.rb', line 32

def store_conversation_or_message(conversation, message)
  if conversation.id.present?
    store_message(conversation, message)
  else
    store_conversation(conversation)
  end
end

#store_message(conversation, message) ⇒ Hash

Store a message in an existing conversation

Parameters:

  • conversation (Conversation)

    The conversation to add the message to

  • message (Message)

    The message to store

Returns:

  • (Hash)

    The API response data



57
58
59
60
61
# File 'lib/ruby_conversations/client.rb', line 57

def store_message(conversation, message)
  response = client.post("api/ai_conversations/#{conversation.id}/ai_messages",
                         { ai_message: message.attributes_for_api })
  handle_response(response)
end