ruby_conversations 1.1.2 → 1.1.3
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 +4 -4
- data/app/models/ruby_conversations/concerns/conversation_templates.rb +107 -0
- data/app/models/ruby_conversations/conversation.rb +1 -0
- data/lib/ruby_conversations/client.rb +9 -1
- data/lib/ruby_conversations/errors.rb +13 -0
- data/lib/ruby_conversations/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65450a67b9c5b73041bae92b1888a1ed62fbaf2e7877cdbf333ffef9f22c66ee
|
4
|
+
data.tar.gz: 60255ec9205d6f84cedb7b9f733fce0121b8f4bc8db4e8adb14cfa58bd745550
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 704bd3d750ae71b30890fe19ab9cbe9d2416b881845eee9193dbb8b6bd5b9f6bdcec232715c82b113064977211e7801b64f9bd00fd7e20d020225895526f851c
|
7
|
+
data.tar.gz: efcda7a901213aa9b5ec5e71196aadc2d319d79432ae21d624d3b3d63e45c8f92079e89041c107c7caf36ca513c7b006969163e88f7effbd3342b219e4f02855
|
@@ -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
|
@@ -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::
|
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
|
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.
|
4
|
+
version: 1.1.3
|
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-
|
11
|
+
date: 2025-05-02 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
|