trix-genius 0.0.13 → 0.1.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/lib/generators/trix_genius/install/install_generator.rb +64 -29
- data/lib/generators/trix_genius/install/templates/trix_genius.rb +4 -4
- data/lib/generators/trix_genius/install/templates/trix_genius_controller.js +1 -1
- data/lib/generators/trix_genius/install/templates/trix_genius_controller.rb +40 -0
- data/spec/generators/trix_genius/install/install_generator_spec.rb +70 -8
- data/spec/tmp/dummy_app/app/controllers/trix_genius_controller.rb +40 -0
- data/spec/tmp/dummy_app/app/javascript/application.js +3 -3
- data/spec/tmp/dummy_app/app/javascript/controllers/application.js +4 -2
- data/spec/tmp/dummy_app/app/javascript/controllers/trix_genius_controller.js +1 -1
- data/spec/tmp/dummy_app/config/initializers/trix_genius.rb +4 -4
- data/spec/tmp/dummy_app/config/routes.rb +17 -0
- metadata +19 -3
- data/spec/tmp/dummy_app/verbose.log +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 05ebdc43ab189099199225ca593632dd48606ed04a3a8c517a538672b4262b2e
|
4
|
+
data.tar.gz: 384e3465038e32372293c6bc3f2a6d9990e78be80226d135251f1f48649b04b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a47ca9c4fa3552d08ff016b1633eccc00d4f5f06b8fb571828e02de7b0aff85ed81e64cac1cdd37d6b1269c9f002e1fcffda80d772980fed4a5e83f03e9b8068
|
7
|
+
data.tar.gz: d781f74a992cfac430634d776fa236ad1a5f9ef10a7417f77cae268afd94a0e9b1ac896b2a64b01fbf1a240e02d3b96fbd9a074db6f74a8b5c169a5ecf6d262c
|
@@ -10,35 +10,28 @@ module TrixGenius
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def add_import_to_application_js
|
13
|
-
create_file "verbose.log", "DEST: #{destination_root}"
|
14
|
-
|
15
13
|
js_application_path = "app/javascript/application.js"
|
16
14
|
js_application_path = File.join(destination_root, js_application_path)
|
17
|
-
application_lines = []
|
18
|
-
application_lines << "// Trix Genius block\n"
|
19
|
-
application_lines << "import \"controllers\""
|
20
|
-
application_lines << "import \"trix\""
|
21
|
-
application_lines << "import \"@rails/actiontext\"\n\n"
|
22
15
|
|
23
16
|
if File.exist?(js_application_path)
|
24
17
|
application_file = File.read(js_application_path)
|
25
|
-
|
18
|
+
|
19
|
+
inject_into_file js_application_path, "\n" + 'import "trix"' + "\n", after: 'import "controllers"'
|
20
|
+
inject_into_file js_application_path, "\n" + 'import "@rails/actiontext"' + ("\n" * 2), after: 'import "trix"'
|
26
21
|
else
|
22
|
+
puts javascript_application_msg
|
27
23
|
say_status("error", "Could not find #{js_application_path}", :red)
|
28
24
|
end
|
29
25
|
|
30
|
-
|
31
26
|
js_application_controller_path = "app/javascript/controllers/application.js"
|
32
27
|
js_application_controller_path = File.join(destination_root, js_application_controller_path)
|
33
|
-
application_controller_lines = []
|
34
|
-
application_controller_lines << "// Trix Genius block"
|
35
|
-
application_controller_lines << "import TrixController from \"controllers/trix-controller\""
|
36
|
-
application_controller_lines << "application.register(\"trix\", TrixController)\n\n"
|
37
28
|
|
38
29
|
if File.exist?(js_application_controller_path)
|
39
30
|
application_controller_file = File.read(js_application_controller_path)
|
40
|
-
|
31
|
+
inject_into_file js_application_controller_path, "\n" + 'import TrixController from "controllers/trix_genius_controller"' + "\n", after: 'import { Application } from "@hotwired/stimulus"'
|
32
|
+
inject_into_file js_application_controller_path, "\n" + 'application.register("trix", TrixController)' + ("\n" * 2), before: 'export { application }'
|
41
33
|
else
|
34
|
+
puts javascript_application_controller_msg
|
42
35
|
say_status("error", "Could not find #{js_application_controller_path}", :red)
|
43
36
|
end
|
44
37
|
end
|
@@ -47,23 +40,65 @@ module TrixGenius
|
|
47
40
|
template "trix_genius_controller.js", File.join(destination_root, "app/javascript/controllers/trix_genius_controller.js")
|
48
41
|
end
|
49
42
|
|
43
|
+
def add_route_to_routes_file
|
44
|
+
route_code = ["",
|
45
|
+
" # TrixGenius: Auto-added route",
|
46
|
+
' post "/trix_genius/correct_spelling", to: "trix_genius#correct_spelling"',
|
47
|
+
""].join("\n")
|
48
|
+
|
49
|
+
inject_into_file File.join(destination_root, "config/routes.rb"), route_code, after: "Rails.application.routes.draw do\n"
|
50
|
+
end
|
51
|
+
|
52
|
+
def create_controller
|
53
|
+
template "trix_genius_controller.rb", File.join(destination_root, "app/controllers/trix_genius_controller.rb")
|
54
|
+
end
|
55
|
+
|
50
56
|
protected
|
51
57
|
|
52
|
-
def
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
58
|
+
def javascript_application_msg
|
59
|
+
<<~MSG
|
60
|
+
|
61
|
+
⚠️ You should create the file manually:
|
62
|
+
|
63
|
+
📄 app/javascript/application.js
|
64
|
+
|
65
|
+
💡 With the following content:
|
66
|
+
|
67
|
+
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
|
68
|
+
import "@hotwired/turbo-rails"
|
69
|
+
import "controllers"
|
70
|
+
|
71
|
+
import "trix"
|
72
|
+
import "@rails/actiontext"
|
73
|
+
|
74
|
+
MSG
|
75
|
+
end
|
76
|
+
|
77
|
+
def javascript_application_controller_msg
|
78
|
+
<<~MSG
|
79
|
+
|
80
|
+
⚠️ You should create the file manually:
|
81
|
+
|
82
|
+
📄 app/javascript/controllers/application.js
|
83
|
+
|
84
|
+
💡 With the following content:
|
85
|
+
|
86
|
+
import { Application } from "@hotwired/stimulus"
|
87
|
+
import TrixController from "controllers/trix-controller"
|
88
|
+
|
89
|
+
const application = Application.start()
|
90
|
+
|
91
|
+
// Configure Stimulus development experience
|
92
|
+
application.debug = false
|
93
|
+
window.Stimulus = application
|
94
|
+
|
95
|
+
application.register("trix", TrixController)
|
96
|
+
|
97
|
+
export { application }
|
98
|
+
MSG
|
99
|
+
|
100
|
+
end
|
101
|
+
|
67
102
|
end
|
68
103
|
end
|
69
104
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
Rails.application.config.deepseek = {
|
2
|
+
api_url: ENV['API_URL'],
|
3
|
+
api_key: ENV['DEEPSEEK_API_KEY']
|
4
|
+
}
|
@@ -33,7 +33,7 @@ export default class extends Controller {
|
|
33
33
|
const content = editor.getDocument().toString(); // Get the current content
|
34
34
|
|
35
35
|
// Send the content to the backend for correction
|
36
|
-
const response = await fetch("/
|
36
|
+
const response = await fetch("/trix_genius/correct_spelling", {
|
37
37
|
method: "POST",
|
38
38
|
headers: { "Content-Type": "application/json" },
|
39
39
|
body: JSON.stringify({ text: content }),
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
class TrixGeniusController < ApplicationController
|
4
|
+
skip_before_action :verify_authenticity_token
|
5
|
+
|
6
|
+
def correct_spelling
|
7
|
+
text = params.require(:text)
|
8
|
+
|
9
|
+
corrected_text = call_ai_service(text)
|
10
|
+
|
11
|
+
render json: { corrected_text: corrected_text }
|
12
|
+
rescue StandardError => e
|
13
|
+
Rails.logger.error("Orthography Correction Error: #{e.message}")
|
14
|
+
render json: { error: "An error occurred while correcting orthography." }, status: :unprocessable_entity
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def call_ai_service(text)
|
20
|
+
headers = {
|
21
|
+
'Content-Type' => 'application/json',
|
22
|
+
'Authorization' => "Bearer #{Rails.application.config.deepseek[:api_key}"
|
23
|
+
}
|
24
|
+
|
25
|
+
body = {
|
26
|
+
model: "deepseek-chat",
|
27
|
+
messages: [{ role: "user", content: "Correct this text: #{text}" }],
|
28
|
+
temperature: 0.7,
|
29
|
+
max_tokens: 500
|
30
|
+
}.to_json
|
31
|
+
|
32
|
+
response = Faraday.post(Rails.application.config.deepseek[:api_url],, body, headers)
|
33
|
+
|
34
|
+
if response.success?
|
35
|
+
return JSON.parse(response.body)['choices'][0]['message']['content'].split('"')[1]
|
36
|
+
else
|
37
|
+
"Error: #{response.status} - #{response.body}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -1,8 +1,9 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
require "generator_spec"
|
3
3
|
require "generators/trix_genius/install/install_generator"
|
4
|
-
require "
|
4
|
+
require "stringio"
|
5
5
|
|
6
|
+
require "pry"
|
6
7
|
|
7
8
|
RSpec.describe TrixGenius::Generators::InstallGenerator, type: :generator do
|
8
9
|
include GeneratorSpec::TestCase
|
@@ -11,13 +12,31 @@ RSpec.describe TrixGenius::Generators::InstallGenerator, type: :generator do
|
|
11
12
|
before(:all) do
|
12
13
|
prepare_destination
|
13
14
|
|
14
|
-
|
15
|
-
|
15
|
+
routes_path = File.join(destination_root, "config/routes.rb")
|
16
|
+
FileUtils.mkdir_p(File.dirname(routes_path))
|
17
|
+
File.write(routes_path, <<~RUBY)
|
18
|
+
Rails.application.routes.draw do
|
19
|
+
resources :posts
|
20
|
+
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
|
21
|
+
|
22
|
+
# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
|
23
|
+
# Can be used by load balancers and uptime monitors to verify that the app is live.
|
24
|
+
|
25
|
+
# Render dynamic PWA files from app/views/pwa/* (remember to link manifest in application.html.erb)
|
26
|
+
# get "manifest" => "rails/pwa#manifest", as: :pwa_manifest
|
27
|
+
# get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker
|
28
|
+
|
29
|
+
# Defines the root path route ("/")
|
30
|
+
# root "posts#index"
|
31
|
+
end
|
32
|
+
RUBY
|
33
|
+
|
16
34
|
js_path = File.join(destination_root, "app/javascript/application.js")
|
17
35
|
js_controller_path = File.join(destination_root, "app/javascript/controllers/application.js")
|
18
36
|
|
19
37
|
FileUtils.mkdir_p(File.dirname(js_path))
|
20
38
|
File.write(js_path, <<~JS)
|
39
|
+
import "@hotwired/turbo-rails"
|
21
40
|
import "controllers"
|
22
41
|
JS
|
23
42
|
|
@@ -25,19 +44,21 @@ RSpec.describe TrixGenius::Generators::InstallGenerator, type: :generator do
|
|
25
44
|
File.write(js_controller_path, <<~JS)
|
26
45
|
import { Application } from "@hotwired/stimulus"
|
27
46
|
const application = Application.start()
|
47
|
+
|
28
48
|
// Configure Stimulus development experience
|
49
|
+
|
29
50
|
application.debug = false
|
30
51
|
window.Stimulus = application
|
31
52
|
export { application }
|
32
53
|
JS
|
33
54
|
end
|
34
55
|
|
35
|
-
it "creates the initializer" do
|
56
|
+
it "Should creates the initializer" do
|
36
57
|
run_generator
|
37
58
|
assert_file "config/initializers/trix_genius.rb"
|
38
59
|
end
|
39
60
|
|
40
|
-
it "updates application.js with import line" do
|
61
|
+
it "Should updates application.js with import line" do
|
41
62
|
run_generator
|
42
63
|
assert_file "app/javascript/application.js" do |content|
|
43
64
|
expect(content).to include('import "trix"')
|
@@ -45,16 +66,57 @@ RSpec.describe TrixGenius::Generators::InstallGenerator, type: :generator do
|
|
45
66
|
end
|
46
67
|
end
|
47
68
|
|
48
|
-
it "updates controller application.js with import line" do
|
69
|
+
it "Should updates controller application.js with import line" do
|
49
70
|
run_generator
|
50
71
|
assert_file "app/javascript/controllers/application.js" do |content|
|
51
|
-
expect(content).to include('import TrixController from "controllers/
|
72
|
+
expect(content).to include('import TrixController from "controllers/trix_genius_controller"')
|
52
73
|
expect(content).to include('application.register("trix", TrixController)')
|
53
74
|
end
|
54
75
|
end
|
55
76
|
|
56
|
-
it "creates the trix_genius_controller.js file" do
|
77
|
+
it "Should creates the trix_genius_controller.js file" do
|
57
78
|
run_generator
|
58
79
|
assert_file "app/javascript/controllers/trix_genius_controller.js"
|
59
80
|
end
|
81
|
+
|
82
|
+
it "Should creates the trix_genius_controller.rb file" do
|
83
|
+
run_generator
|
84
|
+
assert_file "app/controllers/trix_genius_controller.rb"
|
85
|
+
end
|
86
|
+
|
87
|
+
it "Should add route to trix functionality" do
|
88
|
+
run_generator
|
89
|
+
assert_file "config/routes.rb" do |content|
|
90
|
+
expect(content).to include('post "/trix_genius/correct_spelling", to: "trix_genius#correct_spelling"')
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
=begin
|
95
|
+
ready to catch terminal resulto to create the Spec when application.js does't exist
|
96
|
+
|
97
|
+
it "Should launch error because app/javascript/application.js doesn't exist" do
|
98
|
+
js_path = File.join(destination_root, "app/javascript/application.js")
|
99
|
+
FileUtils.rm_f(js_path)
|
100
|
+
|
101
|
+
output = capture_stdout { run_generator }
|
102
|
+
|
103
|
+
binding.pry
|
104
|
+
expect(output).to include("You should create the file manually:")
|
105
|
+
expect(output).to include("app/javascript/application.js")
|
106
|
+
expect(output).to include("import \"trix\"")
|
107
|
+
expect(output).to include("Could not find #{js_path}")
|
108
|
+
end
|
109
|
+
|
110
|
+
def capture_stdout
|
111
|
+
previous_stdout = $stdout
|
112
|
+
fake = StringIO.new
|
113
|
+
$stdout = fake
|
114
|
+
yield
|
115
|
+
fake.string
|
116
|
+
ensure
|
117
|
+
$stdout = previous_stdout
|
118
|
+
end
|
119
|
+
|
120
|
+
=end
|
121
|
+
|
60
122
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
class TrixGeniusController < ApplicationController
|
4
|
+
skip_before_action :verify_authenticity_token
|
5
|
+
|
6
|
+
def correct_spelling
|
7
|
+
text = params.require(:text)
|
8
|
+
|
9
|
+
corrected_text = call_ai_service(text)
|
10
|
+
|
11
|
+
render json: { corrected_text: corrected_text }
|
12
|
+
rescue StandardError => e
|
13
|
+
Rails.logger.error("Orthography Correction Error: #{e.message}")
|
14
|
+
render json: { error: "An error occurred while correcting orthography." }, status: :unprocessable_entity
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def call_ai_service(text)
|
20
|
+
headers = {
|
21
|
+
'Content-Type' => 'application/json',
|
22
|
+
'Authorization' => "Bearer #{Rails.application.config.deepseek[:api_key}"
|
23
|
+
}
|
24
|
+
|
25
|
+
body = {
|
26
|
+
model: "deepseek-chat",
|
27
|
+
messages: [{ role: "user", content: "Correct this text: #{text}" }],
|
28
|
+
temperature: 0.7,
|
29
|
+
max_tokens: 500
|
30
|
+
}.to_json
|
31
|
+
|
32
|
+
response = Faraday.post(Rails.application.config.deepseek[:api_url],, body, headers)
|
33
|
+
|
34
|
+
if response.success?
|
35
|
+
return JSON.parse(response.body)['choices'][0]['message']['content'].split('"')[1]
|
36
|
+
else
|
37
|
+
"Error: #{response.status} - #{response.body}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -1,11 +1,13 @@
|
|
1
1
|
import { Application } from "@hotwired/stimulus"
|
2
|
+
import TrixController from "controllers/trix_genius_controller"
|
3
|
+
|
2
4
|
const application = Application.start()
|
5
|
+
|
3
6
|
// Configure Stimulus development experience
|
7
|
+
|
4
8
|
application.debug = false
|
5
9
|
window.Stimulus = application
|
6
10
|
|
7
|
-
// Trix Genius block
|
8
|
-
import TrixController from "controllers/trix-controller"
|
9
11
|
application.register("trix", TrixController)
|
10
12
|
|
11
13
|
export { application }
|
@@ -33,7 +33,7 @@ export default class extends Controller {
|
|
33
33
|
const content = editor.getDocument().toString(); // Get the current content
|
34
34
|
|
35
35
|
// Send the content to the backend for correction
|
36
|
-
const response = await fetch("/
|
36
|
+
const response = await fetch("/trix_genius/correct_spelling", {
|
37
37
|
method: "POST",
|
38
38
|
headers: { "Content-Type": "application/json" },
|
39
39
|
body: JSON.stringify({ text: content }),
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
Rails.application.config.deepseek = {
|
2
|
+
api_url: ENV['API_URL'],
|
3
|
+
api_key: ENV['DEEPSEEK_API_KEY']
|
4
|
+
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
Rails.application.routes.draw do
|
2
|
+
|
3
|
+
# TrixGenius: Auto-added route
|
4
|
+
post "/trix_genius/correct_spelling", to: "trix_genius#correct_spelling"
|
5
|
+
resources :posts
|
6
|
+
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
|
7
|
+
|
8
|
+
# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
|
9
|
+
# Can be used by load balancers and uptime monitors to verify that the app is live.
|
10
|
+
|
11
|
+
# Render dynamic PWA files from app/views/pwa/* (remember to link manifest in application.html.erb)
|
12
|
+
# get "manifest" => "rails/pwa#manifest", as: :pwa_manifest
|
13
|
+
# get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker
|
14
|
+
|
15
|
+
# Defines the root path route ("/")
|
16
|
+
# root "posts#index"
|
17
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trix-genius
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Giménez Silva Germán Alberto https://rubystacknews.com/
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-04-
|
10
|
+
date: 2025-04-10 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: rails
|
@@ -57,6 +57,20 @@ dependencies:
|
|
57
57
|
- - "~>"
|
58
58
|
- !ruby/object:Gem::Version
|
59
59
|
version: '8.0'
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: faraday
|
62
|
+
requirement: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - "~>"
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '2.12'
|
67
|
+
type: :runtime
|
68
|
+
prerelease: false
|
69
|
+
version_requirements: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - "~>"
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '2.12'
|
60
74
|
- !ruby/object:Gem::Dependency
|
61
75
|
name: generator_spec
|
62
76
|
requirement: !ruby/object:Gem::Requirement
|
@@ -81,15 +95,17 @@ files:
|
|
81
95
|
- lib/generators/trix_genius/install/install_generator.rb
|
82
96
|
- lib/generators/trix_genius/install/templates/trix_genius.rb
|
83
97
|
- lib/generators/trix_genius/install/templates/trix_genius_controller.js
|
98
|
+
- lib/generators/trix_genius/install/templates/trix_genius_controller.rb
|
84
99
|
- lib/trix-genius.rb
|
85
100
|
- lib/trix_genius/engine.rb
|
86
101
|
- spec/generators/trix_genius/install/install_generator_spec.rb
|
87
102
|
- spec/spec_helper.rb
|
103
|
+
- spec/tmp/dummy_app/app/controllers/trix_genius_controller.rb
|
88
104
|
- spec/tmp/dummy_app/app/javascript/application.js
|
89
105
|
- spec/tmp/dummy_app/app/javascript/controllers/application.js
|
90
106
|
- spec/tmp/dummy_app/app/javascript/controllers/trix_genius_controller.js
|
91
107
|
- spec/tmp/dummy_app/config/initializers/trix_genius.rb
|
92
|
-
- spec/tmp/dummy_app/
|
108
|
+
- spec/tmp/dummy_app/config/routes.rb
|
93
109
|
homepage: https://rubystacknews.com/
|
94
110
|
licenses:
|
95
111
|
- GNU
|
@@ -1 +0,0 @@
|
|
1
|
-
DEST: /app/generator/trix-genius/spec/tmp/dummy_app
|