ruby_conversations 1.1.2 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ebe08b35dd3c009e6bd81c5fb6bbb8e9d109d51b76d4469a8af13deeadaebfc5
4
- data.tar.gz: db51099d58654198bcbb1d0c873896f729170937798b0f8cee7928cc44dc5e3c
3
+ metadata.gz: de50b39450657d77806041a0073182e241854dec654df89dafffb1602d7cda6c
4
+ data.tar.gz: e0e5f774586fafcbd4a338b329f59f4b8d099dd72fa9b71e971088c623594abf
5
5
  SHA512:
6
- metadata.gz: 78866ece10dffbce9e4daaa3b6fff1dd8b019663b5c0f665f4043b984b8de17a4c004ae23e62a490eae3626540be14f0811a3b9fef7b00c18f7593789dced64a
7
- data.tar.gz: 84cd8cfe96fad9343323ecd5080586647ab2bb762290954b2aa8887c875f628d96c12d6b61a4473489ea584c2b2cb5749b35cb5632f129e71e5b903a5c890666
6
+ metadata.gz: 23b48547f09522817cea08727abdfe32985eae70b456c738882cf8a10467c480a0274263fea8d624456a5aa1d600965048f2447df859c70097edd091eaf96e48
7
+ data.tar.gz: 680bb8388b484a375ed29dc218f4ad06211cff5d35b6f1567ff910b267adcbd8126a9bec122eb4aa1e72b8e3b3f8a580de50ee5ae6d5e2ca921e6acd48f4b443
@@ -18,12 +18,14 @@ module RubyConversations
18
18
  end
19
19
 
20
20
  def with_prompts(prompt_inputs, description: nil)
21
- MessageBuilder.new(self).build_from_multiple_prompts(prompt_inputs, description: description)
21
+ message = MessageBuilder.new(self).build_from_multiple_prompts(prompt_inputs, description: description)
22
+ chat.with_model(message.message_prompts.first.llm) if message.message_prompts.first.llm.present?
22
23
  self
23
24
  end
24
25
 
25
26
  def with_prompt(name, description: nil, inputs: {})
26
- MessageBuilder.new(self).build_from_single_prompt(name, description: description, inputs: inputs)
27
+ message = MessageBuilder.new(self).build_from_single_prompt(name, description: description, inputs: inputs)
28
+ chat.with_model(message.message_prompts.first.llm) if message.message_prompts.first.llm.present?
27
29
  self
28
30
  end
29
31
 
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyConversations
4
+ module Concerns
5
+ # Handles template-related functionality for Conversation
6
+ module ConversationTemplates
7
+ extend ActiveSupport::Concern
8
+
9
+ # Fetch template metadata for a given template name
10
+ # @param template_name [String] The name of the template to fetch
11
+ # @return [Hash] Template metadata including fields and UI configuration
12
+ def template_for(template_name)
13
+ begin
14
+ template_data = RubyConversations.client.fetch_conversation_template(template_name)
15
+ rescue RubyConversations::ClientError => e
16
+ raise RubyConversations::TemplateNotFoundError, "Template #{template_name} not found" if e.status_code == 404
17
+
18
+ raise e
19
+ end
20
+ extract_template_metadata(template_data)
21
+ end
22
+
23
+ private
24
+
25
+ def extract_template_metadata(template_data)
26
+ return nil unless template_data
27
+
28
+ build_template_metadata(template_data)
29
+ end
30
+
31
+ def build_template_metadata(template_data)
32
+ base_metadata(template_data).merge(ui_metadata(template_data))
33
+ end
34
+
35
+ def base_metadata(template_data)
36
+ {
37
+ id: template_data['template_name'],
38
+ name: template_display_name(template_data),
39
+ description: template_data['template_description'] || "Template: #{template_data['template_name']}",
40
+ fields: extract_fields_from_template(template_data)
41
+ }
42
+ end
43
+
44
+ def ui_metadata(template_data)
45
+ {
46
+ icon: template_data['template_icon'] || 'fa-light fa-comment',
47
+ color: template_data['template_color'] || 'blue',
48
+ response_enabled: template_response_enabled?(template_data),
49
+ cta: template_data['template_call_to_action'] || 'Submit'
50
+ }
51
+ end
52
+
53
+ def template_display_name(template_data)
54
+ prompt_name = extract_prompt_name(template_data)
55
+ template_data['template_display_name'] || default_display_name(prompt_name, template_data)
56
+ end
57
+
58
+ def extract_prompt_name(template_data)
59
+ return nil unless template_data['ai_messages']
60
+ return nil unless template_data['ai_messages'].first
61
+
62
+ prompts = template_data['ai_messages'].first['ai_message_prompts']
63
+ prompts&.first&.dig('name')
64
+ end
65
+
66
+ def default_display_name(prompt_name, template_data)
67
+ return prompt_name.titleize if prompt_name
68
+
69
+ template_data['template_name'].titleize
70
+ end
71
+
72
+ def template_response_enabled?(template_data)
73
+ template_data['template_response_enabled'].nil? ||
74
+ template_data['template_response_enabled']
75
+ end
76
+
77
+ def extract_fields_from_template(template_data)
78
+ return [] unless message_inputs?(template_data)
79
+
80
+ inputs = get_message_inputs(template_data)
81
+ inputs.map do |input|
82
+ {
83
+ name: input['placeholder_name'],
84
+ type: input['field_type'] || 'text',
85
+ label: input['label'] || input['placeholder_name'].titleize
86
+ }
87
+ end
88
+ end
89
+
90
+ def message_inputs?(template_data)
91
+ return false unless template_data['ai_messages']
92
+ return false unless template_data['ai_messages'].first
93
+
94
+ prompts = template_data['ai_messages'].first['ai_message_prompts']
95
+ return false unless prompts&.first
96
+
97
+ prompts.first['ai_message_inputs'].present?
98
+ end
99
+
100
+ def get_message_inputs(template_data)
101
+ return [] unless message_inputs?(template_data)
102
+
103
+ template_data['ai_messages'].first['ai_message_prompts'].first['ai_message_inputs']
104
+ end
105
+ end
106
+ end
107
+ end
@@ -8,6 +8,7 @@ module RubyConversations
8
8
  include ActiveModel::Model
9
9
  include RubyConversations::Concerns::ConversationChat
10
10
  include RubyConversations::Concerns::ConversationMessages
11
+ include RubyConversations::Concerns::ConversationTemplates
11
12
  include RubyConversations::Concerns::LlmCredentials
12
13
  include RubyConversations::Concerns::MessageValidation
13
14
  include RubyConversations::Concerns::MessageProcessing
@@ -68,6 +68,7 @@ module RubyConversations
68
68
  prompt_attrs[:message] = message
69
69
  prompt_attrs[:name] = prompt.name
70
70
  prompt_attrs[:role] = prompt.role
71
+ prompt_attrs[:llm] = prompt.llm
71
72
  prompt = RubyConversations::MessagePrompt.new(prompt_attrs)
72
73
  message.message_prompts << prompt
73
74
  prompt
@@ -10,7 +10,7 @@ module RubyConversations
10
10
 
11
11
  # Define attributes
12
12
  attr_accessor :id, :message_id, :prompt_version_id, :name, :role, :content, :metadata,
13
- :created_at, :updated_at, :message_inputs, :draft, :message
13
+ :created_at, :updated_at, :message_inputs, :draft, :message, :llm
14
14
 
15
15
  # Define nested attributes writer
16
16
  def message_inputs_attributes=(attributes)
@@ -9,7 +9,7 @@ module RubyConversations
9
9
 
10
10
  # Define attributes
11
11
  attr_accessor :id, :name, :role, :message, :valid_placeholders, :temperature, :metadata, :created_at, :updated_at,
12
- :latest_version_id
12
+ :latest_version_id, :llm
13
13
 
14
14
  # Constants
15
15
  ROLES = %w[system user assistant].freeze
@@ -10,7 +10,7 @@ module RubyConversations
10
10
  attr_reader :client
11
11
 
12
12
  PROMPT_ATTRIBUTES = %w[
13
- id name message role temperature valid_placeholders created_at updated_at latest_version_id
13
+ id name message role temperature valid_placeholders created_at updated_at latest_version_id llm
14
14
  ].freeze
15
15
 
16
16
  # Initialize a new API client
@@ -60,6 +60,14 @@ module RubyConversations
60
60
  handle_response(response)
61
61
  end
62
62
 
63
+ # Fetch a conversation template by name
64
+ # @param template_name [String] The name of the template to fetch
65
+ # @return [Hash] The template data including messages and prompts
66
+ def fetch_conversation_template(template_name)
67
+ response = client.get("api/ai_conversations/#{template_name}")
68
+ handle_response(response)
69
+ end
70
+
63
71
  # Fetch a prompt by name
64
72
  # @param name [String] The name of the prompt to fetch
65
73
  # @return [Hash] The prompt attributes
@@ -119,7 +127,7 @@ module RubyConversations
119
127
  return response.body if response.success?
120
128
 
121
129
  @logger.error("API request failed: #{response.body.inspect}")
122
- raise RubyConversations::Error, "API request failed: #{response.body}"
130
+ raise RubyConversations::ClientError.new("API request failed: #{response.body}", status_code: response.status)
123
131
  end
124
132
  end
125
133
  end
@@ -6,4 +6,17 @@ module RubyConversations
6
6
 
7
7
  # Error raised when there is a configuration issue
8
8
  class ConfigurationError < Error; end
9
+
10
+ # Error raised when there is an error with the client
11
+ class ClientError < StandardError
12
+ attr_reader :status_code
13
+
14
+ def initialize(message, status_code: nil)
15
+ super(message)
16
+ @status_code = status_code
17
+ end
18
+ end
19
+
20
+ # Error raised when a template is not found
21
+ class TemplateNotFoundError < Error; end
9
22
  end
@@ -3,7 +3,7 @@
3
3
  module RubyConversations
4
4
  MAJOR = 1
5
5
  MINOR = 1
6
- PATCH = 2
6
+ PATCH = 4
7
7
 
8
8
  VERSION = "#{RubyConversations::MAJOR}.#{RubyConversations::MINOR}.#{RubyConversations::PATCH}".freeze
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_conversations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Shippy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-05-01 00:00:00.000000000 Z
11
+ date: 2025-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -258,6 +258,7 @@ files:
258
258
  - README.md
259
259
  - app/models/ruby_conversations/concerns/conversation_chat.rb
260
260
  - app/models/ruby_conversations/concerns/conversation_messages.rb
261
+ - app/models/ruby_conversations/concerns/conversation_templates.rb
261
262
  - app/models/ruby_conversations/concerns/llm_credentials.rb
262
263
  - app/models/ruby_conversations/concerns/message_api_attributes.rb
263
264
  - app/models/ruby_conversations/concerns/message_attributes.rb