polylingo_chat 0.1.1 → 0.4.0
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/README.md +383 -22
- data/lib/generators/polylingo_chat/install/README +72 -0
- data/lib/generators/polylingo_chat/install/install_generator.rb +43 -15
- data/lib/generators/polylingo_chat/install/templates/channels/polylingo_chat_channel.rb +1 -1
- data/lib/generators/polylingo_chat/install/templates/controllers/polylingo_chat/conversations_controller.rb +127 -0
- data/lib/generators/polylingo_chat/install/templates/controllers/polylingo_chat/messages_controller.rb +113 -0
- data/lib/generators/polylingo_chat/install/templates/create_conversations.rb +2 -2
- data/lib/generators/polylingo_chat/install/templates/create_message_translations.rb +13 -0
- data/lib/generators/polylingo_chat/install/templates/create_messages.rb +8 -4
- data/lib/generators/polylingo_chat/install/templates/create_participants.rb +8 -5
- data/lib/generators/polylingo_chat/install/templates/javascript/chat.js +5 -5
- data/lib/generators/polylingo_chat/install/templates/jobs/translate_job.rb +93 -0
- data/lib/generators/polylingo_chat/install/templates/models/conversation.rb +34 -5
- data/lib/generators/polylingo_chat/install/templates/models/message.rb +16 -8
- data/lib/generators/polylingo_chat/install/templates/models/participant.rb +19 -4
- data/lib/generators/polylingo_chat/install/templates/views/polylingo_chat/conversations/index.html.erb +56 -0
- data/lib/generators/polylingo_chat/install/templates/views/polylingo_chat/conversations/show.html.erb +141 -0
- data/lib/polylingo_chat/version.rb +1 -1
- data/lib/polylingo_chat.rb +0 -1
- metadata +11 -4
- data/lib/polylingo_chat/translate_job.rb +0 -65
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
<div class="polylingo-conversation-show">
|
|
2
|
+
<h1><%%= @conversation.title || "Conversation ##{@conversation.id}" %></h1>
|
|
3
|
+
|
|
4
|
+
<div class="conversation-info">
|
|
5
|
+
<p>
|
|
6
|
+
<strong>Participants:</strong>
|
|
7
|
+
<%%= @conversation.participantables.map { |p| p.try(:name) || p.try(:email) }.join(', ') %>
|
|
8
|
+
</p>
|
|
9
|
+
</div>
|
|
10
|
+
|
|
11
|
+
<div id="messages" class="messages-container">
|
|
12
|
+
<%% @conversation.messages.order(created_at: :asc).each do |message| %>
|
|
13
|
+
<div class="message <%%= message.sender == current_user ? 'sent' : 'received' %>" data-message-id="<%%= message.id %>">
|
|
14
|
+
<div class="message-sender"><%%= message.sender_name %></div>
|
|
15
|
+
<div class="message-body"><%%= message.translated_body || message.body %></div>
|
|
16
|
+
<div class="message-time"><%%= time_ago_in_words(message.created_at) %> ago</div>
|
|
17
|
+
</div>
|
|
18
|
+
<%% end %>
|
|
19
|
+
</div>
|
|
20
|
+
|
|
21
|
+
<!-- Message input form -->
|
|
22
|
+
<div class="message-form">
|
|
23
|
+
<%%= form_with url: polylingo_chat_conversation_messages_path(@conversation), method: :post, local: true, class: "new-message-form" do |f| %>
|
|
24
|
+
<div class="form-group">
|
|
25
|
+
<%%= text_area_tag "message[body]", nil, placeholder: "Type your message...", class: "message-input", rows: 3 %>
|
|
26
|
+
<%%= hidden_field_tag :sender_type, current_user.class.name %>
|
|
27
|
+
<%%= hidden_field_tag :sender_id, current_user.id %>
|
|
28
|
+
</div>
|
|
29
|
+
<%%= f.submit "Send", class: "btn-send" %>
|
|
30
|
+
<%% end %>
|
|
31
|
+
</div>
|
|
32
|
+
|
|
33
|
+
<p>
|
|
34
|
+
<%%= link_to "← Back to Conversations", polylingo_chat_conversations_path, class: "btn-back" %>
|
|
35
|
+
</p>
|
|
36
|
+
</div>
|
|
37
|
+
|
|
38
|
+
<script>
|
|
39
|
+
// Set up for real-time chat via ActionCable
|
|
40
|
+
window.conversationId = <%%= @conversation.id %>;
|
|
41
|
+
window.currentUserId = <%%= current_user&.id || 'null' %>;
|
|
42
|
+
</script>
|
|
43
|
+
|
|
44
|
+
<style>
|
|
45
|
+
.polylingo-conversation-show {
|
|
46
|
+
max-width: 800px;
|
|
47
|
+
margin: 0 auto;
|
|
48
|
+
padding: 20px;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.conversation-info {
|
|
52
|
+
padding: 10px;
|
|
53
|
+
background: #f0f0f0;
|
|
54
|
+
border-radius: 5px;
|
|
55
|
+
margin-bottom: 20px;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
.messages-container {
|
|
59
|
+
min-height: 400px;
|
|
60
|
+
max-height: 600px;
|
|
61
|
+
overflow-y: auto;
|
|
62
|
+
padding: 20px;
|
|
63
|
+
border: 1px solid #ddd;
|
|
64
|
+
border-radius: 8px;
|
|
65
|
+
background: #fff;
|
|
66
|
+
margin-bottom: 20px;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
.message {
|
|
70
|
+
margin-bottom: 15px;
|
|
71
|
+
padding: 10px;
|
|
72
|
+
border-radius: 8px;
|
|
73
|
+
max-width: 70%;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
.message.sent {
|
|
77
|
+
margin-left: auto;
|
|
78
|
+
background: #007bff;
|
|
79
|
+
color: white;
|
|
80
|
+
text-align: right;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
.message.received {
|
|
84
|
+
background: #f1f1f1;
|
|
85
|
+
color: #333;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
.message-sender {
|
|
89
|
+
font-weight: bold;
|
|
90
|
+
font-size: 0.9em;
|
|
91
|
+
margin-bottom: 5px;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
.message-body {
|
|
95
|
+
margin-bottom: 5px;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
.message-time {
|
|
99
|
+
font-size: 0.8em;
|
|
100
|
+
opacity: 0.7;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
.message-form {
|
|
104
|
+
background: #f9f9f9;
|
|
105
|
+
padding: 15px;
|
|
106
|
+
border-radius: 8px;
|
|
107
|
+
margin-bottom: 20px;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
.message-input {
|
|
111
|
+
width: 100%;
|
|
112
|
+
padding: 10px;
|
|
113
|
+
border: 1px solid #ddd;
|
|
114
|
+
border-radius: 5px;
|
|
115
|
+
margin-bottom: 10px;
|
|
116
|
+
font-family: inherit;
|
|
117
|
+
resize: vertical;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
.btn-send {
|
|
121
|
+
background: #007bff;
|
|
122
|
+
color: white;
|
|
123
|
+
padding: 10px 20px;
|
|
124
|
+
border: none;
|
|
125
|
+
border-radius: 5px;
|
|
126
|
+
cursor: pointer;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
.btn-send:hover {
|
|
130
|
+
background: #0056b3;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
.btn-back {
|
|
134
|
+
color: #007bff;
|
|
135
|
+
text-decoration: none;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
.btn-back:hover {
|
|
139
|
+
text-decoration: underline;
|
|
140
|
+
}
|
|
141
|
+
</style>
|
data/lib/polylingo_chat.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: polylingo_chat
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Shoaib Malik
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2025-11-
|
|
11
|
+
date: 2025-11-18 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rails
|
|
@@ -66,27 +66,33 @@ extensions: []
|
|
|
66
66
|
extra_rdoc_files: []
|
|
67
67
|
files:
|
|
68
68
|
- README.md
|
|
69
|
+
- lib/generators/polylingo_chat/install/README
|
|
69
70
|
- lib/generators/polylingo_chat/install/install_generator.rb
|
|
70
71
|
- lib/generators/polylingo_chat/install/templates/README
|
|
71
72
|
- lib/generators/polylingo_chat/install/templates/channels/application_cable/channel.rb
|
|
72
73
|
- lib/generators/polylingo_chat/install/templates/channels/application_cable/connection.rb
|
|
73
74
|
- lib/generators/polylingo_chat/install/templates/channels/polylingo_chat_channel.rb
|
|
75
|
+
- lib/generators/polylingo_chat/install/templates/controllers/polylingo_chat/conversations_controller.rb
|
|
76
|
+
- lib/generators/polylingo_chat/install/templates/controllers/polylingo_chat/messages_controller.rb
|
|
74
77
|
- lib/generators/polylingo_chat/install/templates/create_conversations.rb
|
|
78
|
+
- lib/generators/polylingo_chat/install/templates/create_message_translations.rb
|
|
75
79
|
- lib/generators/polylingo_chat/install/templates/create_messages.rb
|
|
76
80
|
- lib/generators/polylingo_chat/install/templates/create_participants.rb
|
|
77
81
|
- lib/generators/polylingo_chat/install/templates/initializer.rb
|
|
78
82
|
- lib/generators/polylingo_chat/install/templates/javascript/channels/consumer.js
|
|
79
83
|
- lib/generators/polylingo_chat/install/templates/javascript/channels/index.js
|
|
80
84
|
- lib/generators/polylingo_chat/install/templates/javascript/chat.js
|
|
85
|
+
- lib/generators/polylingo_chat/install/templates/jobs/translate_job.rb
|
|
81
86
|
- lib/generators/polylingo_chat/install/templates/models/conversation.rb
|
|
82
87
|
- lib/generators/polylingo_chat/install/templates/models/message.rb
|
|
83
88
|
- lib/generators/polylingo_chat/install/templates/models/participant.rb
|
|
89
|
+
- lib/generators/polylingo_chat/install/templates/views/polylingo_chat/conversations/index.html.erb
|
|
90
|
+
- lib/generators/polylingo_chat/install/templates/views/polylingo_chat/conversations/show.html.erb
|
|
84
91
|
- lib/polylingo_chat.rb
|
|
85
92
|
- lib/polylingo_chat/config.rb
|
|
86
93
|
- lib/polylingo_chat/engine.rb
|
|
87
94
|
- lib/polylingo_chat/railtie.rb
|
|
88
95
|
- lib/polylingo_chat/realtime.rb
|
|
89
|
-
- lib/polylingo_chat/translate_job.rb
|
|
90
96
|
- lib/polylingo_chat/translator.rb
|
|
91
97
|
- lib/polylingo_chat/translator/anthropic_client.rb
|
|
92
98
|
- lib/polylingo_chat/translator/base.rb
|
|
@@ -115,5 +121,6 @@ requirements: []
|
|
|
115
121
|
rubygems_version: 3.4.10
|
|
116
122
|
signing_key:
|
|
117
123
|
specification_version: 4
|
|
118
|
-
summary: Realtime chat with automatic AI translation for Ruby/Rails apps
|
|
124
|
+
summary: Realtime chat with automatic AI translation for Ruby/Rails apps - supports
|
|
125
|
+
API-only mode and polymorphic associations
|
|
119
126
|
test_files: []
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
module PolylingoChat
|
|
2
|
-
class TranslateJob < ApplicationJob
|
|
3
|
-
queue_as :polylingo_chat_translations
|
|
4
|
-
|
|
5
|
-
def perform(message_id)
|
|
6
|
-
# Host app must implement Message model with associations matching spec
|
|
7
|
-
message = ::Message.find_by(id: message_id)
|
|
8
|
-
return unless message
|
|
9
|
-
|
|
10
|
-
conversation = message.conversation
|
|
11
|
-
return unless conversation
|
|
12
|
-
|
|
13
|
-
recipients = conversation.users.where.not(id: message.sender_id)
|
|
14
|
-
|
|
15
|
-
# Check if translation is enabled (API key present)
|
|
16
|
-
translation_enabled = PolylingoChat.config.api_key.present?
|
|
17
|
-
|
|
18
|
-
recipients.each do |recipient|
|
|
19
|
-
if translation_enabled
|
|
20
|
-
# Translation enabled - translate message
|
|
21
|
-
target_lang = recipient.preferred_language || PolylingoChat.config.default_language
|
|
22
|
-
source_lang = PolylingoChat::Translator.detect_language(message.body)
|
|
23
|
-
context = conversation.messages.order(created_at: :asc).last(20).pluck(:body).join("") rescue nil
|
|
24
|
-
|
|
25
|
-
translated = PolylingoChat::Translator.translate(text: message.body, from: source_lang, to: target_lang, context: context)
|
|
26
|
-
# If translator returns a Future-like, wait
|
|
27
|
-
translated = translated.value if translated.respond_to?(:value)
|
|
28
|
-
else
|
|
29
|
-
# No API key - just use original message (chat-only mode)
|
|
30
|
-
translated = message.body
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
# Broadcast to recipient using ActionCable
|
|
34
|
-
begin
|
|
35
|
-
# Broadcast to user-specific channel
|
|
36
|
-
ActionCable.server.broadcast("polylingo_chat_recipient_#{recipient.id}", {
|
|
37
|
-
message: translated,
|
|
38
|
-
original: message.body,
|
|
39
|
-
message_id: message.id,
|
|
40
|
-
sender_id: message.sender_id,
|
|
41
|
-
sender_name: message.sender.name,
|
|
42
|
-
translated: translation_enabled
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
# Also broadcast to conversation channel for demo/group chat
|
|
46
|
-
ActionCable.server.broadcast("conversation_#{conversation.id}", {
|
|
47
|
-
message: translated,
|
|
48
|
-
original: message.body,
|
|
49
|
-
message_id: message.id,
|
|
50
|
-
sender_id: message.sender_id,
|
|
51
|
-
sender_name: message.sender.name,
|
|
52
|
-
translated: translation_enabled,
|
|
53
|
-
recipient_id: recipient.id
|
|
54
|
-
})
|
|
55
|
-
rescue StandardError => e
|
|
56
|
-
Rails.logger.error("PolylingoChat: Broadcast failed - #{e.message}")
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
message.update(translated: translation_enabled)
|
|
61
|
-
rescue StandardError => e
|
|
62
|
-
Rails.logger.error("PolylingoChat: Failed to update message translation status - #{e.message}")
|
|
63
|
-
end
|
|
64
|
-
end
|
|
65
|
-
end
|